CanvasRenderingContext2D.drawImage()

您所在的位置:网站首页 zh-cn_image_0000001086185096png CanvasRenderingContext2D.drawImage()

CanvasRenderingContext2D.drawImage()

2023-03-24 04:43| 来源: 网络整理| 查看: 265

drawImage() 方法在绘制时使用源元素的固有尺寸(以 CSS 像素为单位)。

例如,如果加载图像并在其构造函数中指定可选的大小参数,则必须使用所创建实例的 naturalWidth 和 naturalHeight 属性来正确计算裁剪和缩放区域等内容,而不是 element.width 和 element.height。如果元素是 元素,则 videoWidth 和 videoHeight 也是如此,依此类推。

HTML JavaScript const canvas = document.getElementById('canvas'); const ctx = canvas.getContext('2d'); const image = new Image(60, 45); // Using optional size for image image.onload = drawImageActualSize; // Draw when image has loaded // Load an image of intrinsic size 300x227 in CSS pixels image.src = 'rhino.jpg'; function drawImageActualSize() { // Use the intrinsic size of image in CSS pixels for the canvas element canvas.width = this.naturalWidth; canvas.height = this.naturalHeight; // Will draw the image as 300x227, ignoring the custom size of 60x45 // given in the constructor ctx.drawImage(this, 0, 0); // To use the custom size we'll have to specify the scale parameters // using the element's width and height properties - lets draw one // on top in the corner: ctx.drawImage(this, 0, 0, this.width, this.height); } 结果


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3