javascript - How to select particular area in canvas? -
i new canvas, have image myimg.jpg, have converted image canvas , trying apply pattern image heel.
i not able it. here screenshot:

how can done.
<div id="myid"> <canvas id="canvaswrapper" width="660" height="540"></canvas> </div> function drawimage(){ var ctx = $("canvas")[0].getcontext("2d"), img = new image(); img.onload = function(){ ctx.drawimage(img, 0, 0, 500, 500); ctx.beginpath(); var img2= new image(); var w; var h; img2.src = "http://www.gravatar.com/avatar/e555bd971bc2f4910893cd5b785c30ff?s=128&d=identicon&r=pg"; var pattern = ctx.createpattern(img2, "repeat"); ctx.fillstyle = pattern; ctx.fillrect(0, 0, w, h); ctx.arc(300,305,50,0,2*math.pi); ctx.fill(); ctx.stroke(); }; img.src = "myimg.jpg"; } drawimage();
you can define area want fill using image mask fits on top of image - step photoshop/gimp.
for example, having shoe as-is:

create mask leaving heal in original position (it makes easier draw in - can crop , draw using offset instead). important: background must transparent:

then super-impose pattern using these steps:
- load pattern , define fill-pattern
- draw mask empty canvas
- optional step: adjust transformations if needed (translate, scale)
- choose composite mode "source-atop"
- fill canvas
- choose composite mode "destination-atop"
- draw main image on top (which show behind mask/pattern)
- optional step: draw in original mask image using blending mode "multiply" add shadow , highlights (does not work in ie). creating illusion of depth. ie, drawing on top using reduced alpha or separate image containing shadows etc. can option
result

example
var ishoe = new image, imask = new image, ipatt = new image, count = 3; ishoe.onload = imask.onload = ipatt.onload = loader; ishoe.src = "http://i.stack.imgur.com/hql1c.png"; imask.src = "http://i.stack.imgur.com/k5xwn.png"; ipatt.src = "http://i.stack.imgur.com/ceq10.png"; function loader() { if (--count) return; // wait until images has loaded var ctx = document.queryselector("canvas").getcontext("2d"), pattern = ctx.createpattern(ipatt, "repeat"); // draw in mask ctx.drawimage(imask, 0, 0); // change comp mode ctx.globalcompositeoperation = "source-atop"; // fill mask ctx.scale(0.5, 0.5); // scale: 0.5 ctx.fillstyle = pattern; // remember double area fill: ctx.fillrect(0, 0, ctx.canvas.width*2, ctx.canvas.height*2); ctx.settransform(1,0,0,1,0,0); // reset transform // draw shoe behind mask ctx.globalcompositeoperation = "destination-atop"; ctx.drawimage(ishoe, 0, 0); // make more realistic, add mask in blend mode (does not work in ie): ctx.globalcompositeoperation = "multiply"; if (ctx.globalcompositeoperation === "multiply") { ctx.drawimage(imask, 0, 0); } } <canvas width=281 height=340></canvas>
Comments
Post a Comment