Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

canvas cut path to image

// canvas related variables
var canvas=document.getElementById("canvas");
var ctx=canvas.getContext("2d");
var cw,ch;
var $canvas=$("#canvas");
var canvasOffset=$canvas.offset();
var offsetX=canvasOffset.left;
var offsetY=canvasOffset.top;

// set some canvas styles
ctx.strokeStyle='black';

// an array to hold user's click-points that define the clipping area
var points=[];

// load the image 
var img=new Image();
img.crossOrigin='anonymous';
img.onload=start;
img.src="https://dl.dropboxusercontent.com/u/139992952/multple/houses1.jpg";
function start(){

  // resize canvas to fit the img
  cw=canvas.width=img.width;
  ch=canvas.height=img.height;

  // draw the image at 25% opacity
  drawImage(0.25);

  // listen for mousedown and button clicks
  $('#canvas').mousedown(function(e){handleMouseDown(e);});
  $('#reset').click(function(){ points.length=0; drawImage(0.25); });
}



function handleMouseDown(e){

  // tell the browser that we're handling this event
  e.preventDefault();
  e.stopPropagation();

  // calculate mouseX & mouseY
  mx=parseInt(e.clientX-offsetX);
  my=parseInt(e.clientY-offsetY);

  // push the clicked point to the points[] array
  points.push({x:mx,y:my});

  // show the user an outline of their current clipping path
  outlineIt();

  // if the user clicked back in the original circle
  // then complete the clip
  if(points.length>1){
    var dx=mx-points[0].x;
    var dy=my-points[0].y;
    if(dx*dx+dy*dy<10*10){
      clipIt();
    }
  }
}


// redraw the image at the specified opacity
function drawImage(alpha){
  ctx.clearRect(0,0,cw,ch);
  ctx.globalAlpha=alpha;
  ctx.drawImage(img,0,0);
  ctx.globalAlpha=1.00;
}

// show the current potential clipping path
function outlineIt(){
  drawImage(0.25);
  ctx.beginPath();
  ctx.moveTo(points[0].x,points[0].y);
  for(var i=0;i<points.length;i++){
    ctx.lineTo(points[i].x,points[i].y);
  }
  ctx.closePath();
  ctx.stroke();
  ctx.beginPath();
  ctx.arc(points[0].x,points[0].y,10,0,Math.PI*2);
  ctx.closePath();
  ctx.stroke();
}

// clip the selected path to a new canvas
function clipIt(){

  // calculate the size of the user's clipping area
  var minX=10000;
  var minY=10000;
  var maxX=-10000;
  var maxY=-10000;
  for(var i=1;i<points.length;i++){
    var p=points[i];
    if(p.x<minX){minX=p.x;}
    if(p.y<minY){minY=p.y;}
    if(p.x>maxX){maxX=p.x;}
    if(p.y>maxY){maxY=p.y;}
  }
  var width=maxX-minX;
  var height=maxY-minY;

  // clip the image into the user's clipping area
  ctx.save();
  ctx.clearRect(0,0,cw,ch);
  ctx.beginPath();
  ctx.moveTo(points[0].x,points[0].y);
  for(var i=1;i<points.length;i++){
    var p=points[i];
    ctx.lineTo(points[i].x,points[i].y);
  }
  ctx.closePath();
  ctx.clip();
  ctx.drawImage(img,0,0);
  ctx.restore();

  // create a new canvas 
  var c=document.createElement('canvas');
  var cx=c.getContext('2d');

  // resize the new canvas to the size of the clipping area
  c.width=width;
  c.height=height;

  // draw the clipped image from the main canvas to the new canvas
  cx.drawImage(canvas, minX,minY,width,height, 0,0,width,height);

  // create a new Image() from the new canvas
  var clippedImage=new Image();
  clippedImage.onload=function(){
    // append the new image to the page
    document.body.appendChild(clippedImage);
  }
  clippedImage.src=c.toDataURL();


  // clear the previous points 
  points.length=0;

  // redraw the image on the main canvas for further clipping
  drawImage(0.25);
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: Why is #_=_ appended to the redirect URI? passport facebook 
Javascript :: how to make sticky footer with react router 
Javascript :: how to fix eslint jsx not allowed in js 
Javascript :: momentDurationFormatSetup 
Javascript :: how enable custom css and js vscode ubuntu 
Javascript :: vscode coderunner does not find python library 
Javascript :: fetch is not defined amazon-cognito-identity-js 
Javascript :: how to change the model object django in javascript 
Javascript :: time second updating without rendering 
Javascript :: button inside popover not viible 
Javascript :: monk find fields 
Javascript :: Enzymes are proteins that speed up reactions by 
Javascript :: simple javascript router 
Javascript :: how to use batch export function forn exports charts in fusioncharts via angularjs 
Javascript :: add object to array setstate 
Javascript :: react native: how to know th softkey height 
Javascript :: postfix increment 
Javascript :: node ja sap concur 
Javascript :: appendchild element once if element presense in js 
Javascript :: if typeof equals array javascript 
Javascript :: curl node exporter 
Javascript :: js nvl function 
Javascript :: write confirm dialog box if yes then run function using php 
Javascript :: puppeteer print page numbers after second page 
Javascript :: how-to-avoid-to-exceed-rate-limit-by-using-discord-js-api 
Javascript :: how to get perticular 5 string element out of 10 from array javascript 
Javascript :: enable clipboard 
Javascript :: textfield label language react 
Javascript :: cypress chai exclude spectial char when asserting 
Javascript :: hot add value in javascript 
ADD CONTENT
Topic
Content
Source link
Name
4+4 =