canvas drawImage方法不显示图片的解决方案

先复习一下用法:

context.drawImage(img,sx,sy,swidth,sheight,x,y,width,height);

各个参数说明:

参数描述
img规定要使用的图像、画布或视频。
sx可选。开始剪切的 x 坐标位置。
sy可选。开始剪切的 y 坐标位置。
swidth可选。被剪切图像的宽度。
sheight可选。被剪切图像的高度。
x在画布上放置图像的 x 坐标位置。
y在画布上放置图像的 y 坐标位置。
width可选。要使用的图像的宽度。(伸展或缩小图像)
height可选。要使用的图像的高度。(伸展或缩小图像)

用一张图来说明:

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

imgSource image objectSprite sheet
sxSource xFrame index times frame width
sySource y0
swSource widthFrame width
shSource heightFrame height
dxDestination x0
dyDestination y0
dwDestination widthFrame width
dhDestination heightFrame height

 

 

 

 

 

 

 

 

 

以下是w3school给的例子:

var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
var img = document.getElementById("tulip");
ctx.drawImage(img,10,10);

然而实际在运行中图片是不显示的,这是比较坑的问题,解决方法是将drawImage放入

img.onload或者window.onload中。

 

参考链接:

https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/drawImage

转载于:https://www.cnblogs.com/undefined000/p/issue_drawImage.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值