【问题标题】:HTML / Java Script Canvas - how to draw an image between source and destination points?HTML / Javascript Canvas - 如何在源点和目标点之间绘制图像?
【发布时间】:2014-11-22 17:12:45
【问题描述】:

我尝试过使用画布的drawImage函数。

在文档(http://msdn.microsoft.com/en-us/library/ie/ff975414(v=vs.85).aspx)中,我以为最后两个参数是目标点,但我猜不是因为它不起作用。

有没有办法在两点之间绘制图像而不旋转它或类似的东西?

谢谢!

【问题讨论】:

标签: javascript html image canvas draw


【解决方案1】:
context.drawImage( 
    sourceImage, 
    sourceX, sourceY, sourceWidthToClip, sourceHeightToClip, 
    canvasX, canvasY, scaledWidth, scaledHeight );

context.drawImage中的第一个参数是源图片。

接下来的 4 个参数是要从该源剪切的 x、y、宽度和高度矩形子图像

最后 4 个参数是要在画布上绘制的 x、y、scaledWidth 和 scaledHeight 矩形缩放图像。

带注释的drawImage:

context.drawImage(

    sourceImage,  // the source image to clip from

    sX,           // the left X position to start clipping 
    sY,           // the top Y position to start clipping
    sW,           // clip this width of pixels from the source
    wH,           // clip this height of pixels from the source

    dX,           // the left X canvas position to start drawing the clipped sub-image
    dY,           // the top Y canvas position to start drawing the clipped sub-image
    dW,           // scale sW to dW and draw a dW wide sub-image on the canvas
    dH            // scale sH to dH and draw a dH high sub-image on the canvas

}

视觉drawImage:

![在此处输入图片描述][1]

drawImage 的代码示例:

var canvas=document.getElementById("canvas");
var ctx=canvas.getContext("2d");
var canvas1=document.getElementById("drawImage");
var ctx1=canvas1.getContext("2d");

var img=new Image();
img.onload=start;
img.src="https://dl.dropboxusercontent.com/u/139992952/stackoverflow/avatars.jpg";
function start(){

  canvas.width=img.width;
  canvas.height=img.height;
  ctx.drawImage(img,0,0);

  var scale=2;
  canvas1.width=471/5*3;
  canvas1.height=255/2*3;

  ctx1.drawImage(img,
                 94,0,94,120,
                 50,50,94*scale,120*scale
                );
}
body{ background-color: ivory; padding:10px; }
canvas{border:1px solid red;}
<h4>The original image</h4>
<canvas id="canvas" width=300 height=300></canvas><br>
<h4>The clipped & scaled sub-image drawn into the canvas</h4>
<canvas id="drawImage" width=300 height=300></canvas>

drawImage 的示例插图:

[补充:如何用html范围控件控制dx,dy]

var canvas=document.getElementById("canvas");
var ctx=canvas.getContext("2d");
var canvas1=document.getElementById("drawImage");
var ctx1=canvas1.getContext("2d");

var $dx=$('#dx');
var $dy=$('#dy');
var scale=2;

var img=new Image();
img.onload=start;
img.src="https://dl.dropboxusercontent.com/u/139992952/stackoverflow/avatars.jpg";
function start(){

  canvas.width=img.width;
  canvas.height=img.height;
  ctx.drawImage(img,0,0);

  canvas1.width=471/5*3;
  canvas1.height=255/2*3;

  draw();

  $dx.change(function(){draw();});
  $dy.change(function(){draw();});

}

function draw(){
  ctx1.clearRect(0,0,canvas1.width,canvas1.height);
  ctx1.drawImage(img,94,0,94,120,$dx.val(),$dy.val(),94*scale,120*scale);
}
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>
<h4>The original image</h4>
<canvas id="canvas" width=300 height=300></canvas><br>
<h4>The clipped & scaled sub-image drawn into the canvas</h4>
dx:<input id=dx type=range min=0 max=280 value=0><br>
dy:<input id=dy type=range min=0 max=380 value=0><br>
<canvas id="drawImage" width=300 height=300></canvas>
[1]:http://i.stack.imgur.com/9paHM.jpg

【讨论】:

  • 谢谢!这对理解非常有用。所以没有办法以某种方式选择目的地x和y?也许是一些 jQuery 插件?
  • 没有 jQuery 插件——html canvas 是一个 javascript 生物,但添加用户控件很容易。我已经添加到我的答案中,以展示如何使用 html 范围控件控制 dx,dy。祝你的项目好运!
  • 这是一个很好的答案。给你的道具,先生。
猜你喜欢
  • 2011-07-23
  • 2014-02-03
  • 2011-10-30
  • 1970-01-01
  • 2014-11-19
  • 1970-01-01
  • 2019-09-20
  • 1970-01-01
  • 2011-05-08
相关资源
最近更新 更多