한 가지 방법은 이미지와 그 이미지
당신은 오프셋 이미지로 동일한 오프셋과 모양을 그리기에 의해 조정 이미지와 그 형태를 유지할 수와 관련된 형태 모두를 그리는 함수를 작성하는 것입니다. 당신의 이미지를 그릴 경우에 따라서 [X, Y]를 당신은 모양으로 각 지점에 X & Y를 추가 :
// points[] is an array of points that defines the shape you
// want drawn on your image
function drawImageWithShapes(img,points,x,y){
ctx.drawImage(img,x,y);
ctx.beginPath();
ctx.moveTo(points[0].x+x,points[0].y+y);
for(var i=1; i<points.length;i++){
ctx.lineTo(points[i].x+x,points[i].y+y);
}
ctx.stroke();
}
var canvas=document.getElementById("canvas");
var ctx=canvas.getContext("2d");
var cw=canvas.width;
var ch=canvas.height;
ctx.lineWidth=4;
ctx.strokeStyle='red';
var x=50;
var y=50;
var points=[];
points.push({x:37,y:0});
points.push({x:75,y:75});
points.push({x:0,y:75});
points.push({x:37,y:0});
var img=new Image();
img.onload=start;
img.src="https://dl.dropboxusercontent.com/u/139992952/multple/sun.png";
function start(){
drawImageWithShapes(img,points,x,y);
}
function drawImageWithShapes(img,points,x,y){
ctx.drawImage(img,x,y);
ctx.beginPath();
ctx.moveTo(points[0].x+x,points[0].y+y);
for(var i=1; i<points.length;i++){
ctx.lineTo(points[i].x+x,points[i].y+y);
}
ctx.stroke();
}
$('#test').click(function(){
x+=10;
y+=10;
ctx.clearRect(0,0,cw,ch);
drawImageWithShapes(img,points,x,y);
});
body{ background-color: ivory; padding:10px; }
#canvas{border:1px solid red;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<button id=test>Move and Redraw</button><br><br>
<canvas id="canvas" width=300 height=300></canvas>
은 가까운 것 같다 내가 뭘 찾고있어, 우리는 마우스 포인터를 사용하여로드하지 않고 런타임에 이미지 위에 선을 그리고 끌기 선과 함께 이미지를 끌 수 있습니까? – NewBee
@NewBee. 물론 ... 마우스를 사용하여 html5 캔버스에 선을 그리는 많은 예제가 있으므로 여기서 다시 설명하지 않겠습니다. 이 링크는 많은 예제 중 하나에 대한 링크입니다. http://stackoverflow.com/questions/23140642/drawing-on-mouse-move-with-canvas-hmtl5/23141469#23141469. 프로젝트에 행운을 빌어 요! – markE
답장을 보내 주셔서 감사합니다. 그러나, 나는 문제를 다시 설명하려고 노력할 것이다. 내가 드래그 할 수있는 페이지의 일부 이미지가 있습니다. 그러면 내가 원하는 것은 드래그 가능한 이미지에 원을 표시 할 것입니다. 그래서, 이미지를 드래그하면 현재 마크 된 원이 드래그됩니다. 나는 지금 그것을 설명 할 수 있었으면 좋겠다. .... 고마워. – NewBee