使用图像创建Canvas元素并附加到父元素 [英] Create Canvas element with image and append to parent

查看:110
本文介绍了使用图像创建Canvas元素并附加到父元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建带有图像的Canvas元素,需要附加到父对象。

I need to create Canvas element with image and need to append to parent for this i have done this

<html>
<head>
    <script>
        window.onload = function() {
        var canvas = document.createElement('canvas');
        var context = canvas.getContext("2d");
        canvas.id = "canvas_id";
        canvas.setAttribute("class" ,"canvas");
        canvas.height =  "400";
        canvas.width =  "800";
        var image = new Image();
        image.src = "http://localhost/tile.png";
        image.onload = function(){
           context.drawImage(image, canvas.width, canvas.height);
        }
        document.body.appendChild(canvas);
    }
</script>
</head>
<body>
</body>
</html>

它会给空白画布

引导我 ?

推荐答案

您使用的是 drawImage()错误的方式。

You are using drawImage() the wrong way. Instead of drawing the image at (0,0) you are drawing it just outside the canvas area as width and height is where position normally goes.

有效的签名是:


context.drawImage(image,dx,dy)

context.drawImage(image,dx,dy,dw ,dh)

context.drawImage(image,sx,sy,sw,sh,dx,dy,dw,dh)

context.drawImage(image, dx, dy)
context.drawImage(image, dx, dy, dw, dh)
context.drawImage(image, sx, sy, sw, sh, dx, dy, dw, dh)

其中 dx dy 是delta位置(相对于原点, 0)。没有width和height指定 drawImage()将默认使用图像的自然宽度和高度。

Where dx and dy are delta position (relative to origin, normally (0,0) when untranslated). Without width and height specified drawImage() will by default use the image's natural width and height.

允许覆盖默认大小,第三个允许您从一个区域绘制到另一个区域。

The second version allows to override the default size, and the third will allow you to draw from one region to another.

来源

更正的示例:

window.onload = function() {
  var canvas = document.createElement('canvas');
  var context = canvas.getContext("2d");
  canvas.id = "canvas_id";
  canvas.className = "canvas";                  // should be className
  canvas.height = 400;                          // should be numbers
  canvas.width = 800;
  var image = new Image();
  image.onload = function() {
    // or set canvas size = image, here: (this = currently loaded image)
    // canvas.width = this.width;
    // canvas.height = this.height;
    context.drawImage(this, 0, 0);              // draw at (0,0), size = image size

    // or, if you want to fill the canvas independent on image size:
    // context.drawImage(this, 0, 0, canvas.width, canvas.height);
  }
  // set src last (recommend to use relative paths where possible)
  image.src = "http://lorempixel.com/output/fashion-q-c-800-400-7.jpg";
  document.body.appendChild(canvas);
}

,如果你只需要附加的图像,则不需要通过画布。只需直接将图像添加到DOM(我假设这不是你想要的,但是在case ..):

That being said, if you only need the image appended there is no need to go via canvas. Just add the image to DOM directly (I assume this is not you want, but just in case..):

var image = new Image();
image.src = "tile.png";
document.body.appendChild(image);

这篇关于使用图像创建Canvas元素并附加到父元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆