-2
A
回答
0
通常我不会回答 “的要求编写代码”,但我有你需要铺设码周围。
我在这里提供它没有进一步的解释或注释:
var canvas1a=document.createElement("canvas");
var ctx1a=canvas1a.getContext("2d");
var canvas2=document.getElementById("canvas2");
var ctx2=canvas2.getContext("2d");
var canvas=document.getElementById("canvas");
var ctx=canvas.getContext("2d");
function reOffset(){
var BB=canvas.getBoundingClientRect();
offsetX=BB.left;
offsetY=BB.top;
}
var offsetX,offsetY;
reOffset();
window.onscroll=function(e){ reOffset(); }
window.onresize=function(e){ reOffset(); }
var canvas1=document.getElementById("canvas1");
var ctx1=canvas1.getContext("2d");
function reOffset1(){
var BB=canvas1.getBoundingClientRect();
offsetX1=BB.left;
offsetY1=BB.top;
}
var offsetX1,offsetY1;
reOffset1();
window.onscroll=function(e){ reOffset1(); }
window.onresize=function(e){ reOffset1(); }
var topLeft,topRight,bottomRight,bottomLeft;
var startX,startY,mouseX,mouseY;
var isDown=false;
var img=new Image();
img.onload=start;
img.src="https://dl.dropboxusercontent.com/u/139992952/multple/tilted.jpg";
function start(){
canvas.width=img.width;
canvas.height=img.height;
ctx.drawImage(img,0,0);
//
$("#canvas").on('mousedown',handleMouseDown);
}
function handleMouseDown(e){
e.preventDefault();
e.stopPropagation();
//
mX=parseInt(e.clientX-offsetX);
mY=parseInt(e.clientY-offsetY);
//
if(!topLeft){
topLeft={x:mX,y:mY};
drawMarker(topLeft);
}else if(!topRight){
topRight={x:mX,y:mY};
drawMarker(topRight);
}else{
$("#canvas").off('mousedown',handleMouseDown);
//
bottomRight={x:mX,y:mY};
drawMarker(bottomRight);
//
var dx=topRight.x-topLeft.x;
var dy=topRight.y-topLeft.y;
var angle=Math.atan2(dy,dx);
var width=Math.sqrt(dx*dx+dy*dy);
dx=bottomRight.x-topRight.x;
dy=bottomRight.y-topRight.y;
var height=Math.sqrt(dx*dx+dy*dy);
//
canvas1a.width=canvas1.width=width;
canvas1a.height=canvas1.height=height;
ctx1a.rotate(-angle);
ctx1a.drawImage(img,-topLeft.x,-topLeft.y);
ctx1.strokeStyle='red';
ctx1.drawImage(canvas1a,0,0);
//
$("#canvas1").mousedown(function(e){handleMouseDown1(e);});
$("#canvas1").mousemove(function(e){handleMouseMove1(e);});
$("#canvas1").mouseup(function(e){handleMouseUpOut1(e);});
$("#canvas1").mouseout(function(e){handleMouseUpOut1(e);});
}
}
function handleMouseDown1(e){
// tell the browser we're handling this event
e.preventDefault();
e.stopPropagation();
//
startX=parseInt(e.clientX-offsetX1);
startY=parseInt(e.clientY-offsetY1);
// set drag flag
isDown=true;
}
function handleMouseUpOut1(e){
if(!isDown){return;}
// tell the browser we're handling this event
e.preventDefault();
e.stopPropagation();
//
mouseX=parseInt(e.clientX-offsetX1);
mouseY=parseInt(e.clientY-offsetY1);
// clear drag flag
isDown=false;
//
var w=mouseX-startX;
var h=mouseY-startY;
canvas2.width=w;
canvas2.height=h;
ctx2.drawImage(canvas1a,startX,startY,w,h,0,0,w,h);
}
function handleMouseMove1(e){
if(!isDown){return;}
// tell the browser we're handling this event
e.preventDefault();
e.stopPropagation();
mouseX=parseInt(e.clientX-offsetX1);
mouseY=parseInt(e.clientY-offsetY1);
ctx1.drawImage(canvas1a,0,0);
ctx1.strokeRect(startX,startY,mouseX-startX,mouseY-startY);
}
function drawMarker(pt){
ctx.beginPath();
ctx.arc(pt.x,pt.y,3,0,Math.PI*2);
ctx.closePath();
ctx.fillStyle='red';
ctx.fill();
}
body {
background-color: ivory;
}
canvas {
border: 1px solid red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<h4>First the topLeft corner<br>then click topRight corner<br>then click bottomRight corner</h4>
<canvas id="canvas" width=300 height=300></canvas><br>
<h4>Drag to clip a part of the image</h4>
<canvas id="canvas1" width=300 height=300></canvas>
<h4>Straightened & clipped image</h4>
<canvas id="canvas2" width=300 height=300></canvas>
相关问题
- 1. 我如何裁剪图像使用HTML5画布和JavaScript为了离线裁剪图像?
- 2. 裁剪图像到画布
- 3. 使用html5和javascript剪裁图像
- 4. html5图像裁剪
- 5. 如何使用drawImage()来裁剪图像?
- 6. 使用画布元素来裁剪图像
- 7. 如何使用PHP调整图像大小和裁剪图像?
- 8. 在HTML5画布中裁剪多个图像
- 9. HTML5裁剪的画布图像不显示
- 10. 使用画布旋转和裁剪图像
- 11. 在HTML5中裁剪图像
- 12. HTML5画布图像裁剪,调整大小,并保存不失品质
- 13. HTML5画布:在单个画布上旋转裁剪后的图像
- 14. 如何使用vb.net和SQL Server来裁剪和调整图像大小
- 15. 如何使用php-imagick来缩放和裁剪图像?
- 16. 无裁剪调整画布的尺寸
- 17. 使用drawImage()画布裁剪图像的倍数?
- 18. 使用javascript来裁剪图像
- 19. 如何在画布上放置裁剪图像? wpf c#
- 20. 如何从视频裁剪画布并保存为图像?
- 21. 调整大小和裁剪图像
- 22. 调整大小和裁剪图像
- 23. 自动裁剪和调整图像
- 24. 裁剪和调整图像大小
- 25. C#裁剪和调整大图像
- 26. 调整大小和裁剪图像
- 27. ASP.NET图像大小调整和裁剪
- 28. VB.NET调整大小和裁剪图像
- 29. PHP图像调整大小和裁剪
- 30. 在Wordpress中裁剪和调整图像
至于轮换,我想这[SO后(HTTP://计算器.com/questions/8829929/how-to-rotate-around-one-specified-point-in-fabric-js)应该有一些帮助..但是根据你的后图,我认为你还需要做一些修剪其中可以找到[这里](http://stackoverflow.com/quest离子/ 18732876 /作物功能使用fabricjs) – vmachan
您可以按照此代码http://jsfiddle.net/v3471fur/ –
你到目前为止尝试过什么。显示那个片段。否则,SO不会为你写代码,这个问题很快就会被删除。 – Rob