javascript - How to draw a line in canvas with a background image -
i'm trying draw line in canvas (which can do), want put repeating pattern on line using background image (unless there way put repeating background image on line in canvas?).
how can draw line background image?
i understand concept of clipping, seems work shapes... not stroke. ideas?
here jsfiddle of trying http://jsfiddle.net/z9cd7/
function (callback) { window.settimeout(callback, 1000 / 60); }; })(); var radius = 50; var x = 100; var dx = 10; var y = 100; var dy = 10; var delay = 10; var img = new image(); img.onload = function () { var canvas1 = document.getelementbyid("image"); var ctximg = canvas1.getcontext("2d"); ctximg.drawimage(img, 0, 0, img.width, img.height, 0, 0, canvas.width, canvas.height); /* ctx.clearrect(0, 0, canvas.width, canvas.height); ctx.save(); ctx.beginpath(); ctx.arc(100, 100, radius, 0, 2 * math.pi, false); ctx.clip(); ctx.drawimage(img, 0, 0, img.width, img.height, 0, 0, canvas.width, canvas.height); ctx.restore(); */ ctx.moveto(0,0) ctx.lineto(100,100) ctx.linewidth = 10; ctx.stroke() ctx.clip(); ctx.drawimage(img, 0, 0, img.width, img.height, 0, 0, canvas.width, canvas.height); ctx.restore(); //animate(); } img.src = "http://lh3.ggpht.com/_z-i7ef_acgi/trxpfywlcxi/aaaaaaaaad8/acsxiuo_c1g/house%20vector.png";
i see 2 solutions :
- build polygon corresponds thick line, fill image pattern. requires compute tangent , little math. wrote small article similar issue here :
http://gamealchemist.wordpress.com/2013/08/28/variable-width-lines-in-html5-canvas/
can see fiddle here : http://jsfiddle.net/gamealchemist/gcwvu/
the code one, want varline w1 = w2 (same start / end thickness) ;
// varline : draws line a(x1,y1) b(x2,y2) // starts w1 width , ends w2 width. // relies on fillstyle color. // ctx valid canvas's context2d. function varline(ctx, x1, y1, x2, y2, w1, w2) { var dx = (x2 - x1); var dy = (y2 - y1); w1 /= 2; // use w1/2 , w2/2 computations. w2 /= 2; // length of ab vector var length = math.sqrt(sq(dx) + sq(dy)); if (!length) return; // exit if 0 length var shiftx = -dy * w1 / length; // compute aa1 vector's x var shifty = dx * w1 / length; // compute aa1 vector's y ctx.beginpath(); ctx.moveto(x1 + shiftx, y1 + shifty); ctx.lineto(x1 - shiftx, y1 - shifty); // draw a1a2 shiftx = -dy * w2 / length; // compute bb1 vector's x shifty = dx * w2 / length; // compute bb1 vector's y ctx.lineto(x2 - shiftx, y2 - shifty); // draw a2b1 ctx.lineto(x2 + shiftx, y2 + shifty); // draw b1b2 ctx.closepath(); // draw b2a1 ctx.fill(); }
- second solution quick : use globalcompositeoperation modes clipping you. instance draw line, use 'source-in', draw image on top of line.
handy, issue here work if canvas clean before line draw. if can choose freely drawing order, not issue, otherwise, you'll have draw line in temp canvas, draw canvas on main canvas.
updated fiddle here : http://jsfiddle.net/gamealchemist/z9cd7/1/
edit : fiddle image repetitions : http://jsfiddle.net/gamealchemist/z9cd7/2/
Comments
Post a Comment