Javascript/HTML5使用图像填充画布
Javascript/HTML5 using image to fill canvas
我正在尝试获取图像来填充我的画布。这是我正在尝试的代码:
var blueprint_background = new Image();
blueprint_background.src = 'images/blueprint_background.png';
var pattern = context.createPattern(blueprint_background, "repeat");
context.fillStyle = pattern;
context.fill();
问题是什么都没有出现。我能够做基本的context.fillRect(..)示例,但这给我带来了麻烦。
谢谢。
您必须等到图像加载,使用图像的 onload 属性知道它何时加载。
var blueprint_background = new Image();
blueprint_background.src = 'images/blueprint_background.png';
blueprint_background.onload = function(){
var pattern = context.createPattern(this, "repeat");
context.fillStyle = pattern;
context.fill();
};
如果有人想从 Worker 中的图像创建模式,请注意。
内部工人类图像未定义!
ReferenceError: Image is not defined
在这种情况下,我们可以执行以下操作:
const result = await fetch('./image.png');
const blob = await result.blob();
const image = await createImageBitmap(blob);
const pattern = canvasContext2D.createPattern(image, 'repeat');
然后只需填写OffscreenCanvasContext2D:
canvasContext2D.fillStyle = pattern;
canvasContext2D.fill();
引用:
- 屏幕外画布 - developer.mozilla.org
- OffscreenCanvasRenderingContext2D - html.spec.whatwg.org
- 工人 - developer.mozilla.org
- 获取 - developer.mozilla.org
- 斑点 - developer.mozilla.org
- 创建图像位图 - developer.mozilla.org
- 创建模式 - html.spec.whatwg.org
相关文章:
- 用图像而不是颜色填充对象(将图像绘制到画布上)
- 如何在d3.js中填充svg圆圈内的图像
- 如何将整个html页面转换为带有填充控件的图像
- 如何使用fabricjs在图像中插入填充颜色
- 设置图像以使用纸张.js填充画布
- Chrome通过自动填充隐藏背景图像
- jQuery统计背景图像的纵横比并更改元素的填充值
- PHP:用颜色填充图像块并保存信息
- 用于在网页上填充图像链接的 JavaScript 代码
- 用HTML和js中的按钮填充图像或矢量路径
- KineticJS -图案和填充图像
- 用ng-flow.js从json中填充图像文件
- 填充图像与其他图像javascript/html5
- 在JS中使用共享类动态地按顺序填充图像
- 设置一个 for 循环来填充图像源
- 回发问题后填充图像
- 在 ajax 调用后,使用新数据重新填充图像选取器选择控件
- 使用JavaScript填充图像(不拉伸)
- 强制布局节点填充图像
- 动态JS:将填充图像应用于多边形形状时,使其褪色