test-skybox/createImage.js
2024-08-13 18:56:27 +08:00

36 lines
1.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

function createImage(width) {
// 创建width*width的jpg格式的图片背景为黑色随机生成1000个像素点颜色为白色大小从1到5像素不等
var canvas = document.createElement('canvas');
canvas.width = width;
canvas.height = width;
var context = canvas.getContext('2d');
context.fillStyle = 'black';
context.fillRect(0, 0, width, width);
for (var i = 0; i < 10000; i++) {
var x = Math.random() * width;
var y = Math.random() * width;
var size = Math.random() * 3;
context.fillStyle = 'white';
context.fillRect(x, y, size, size);
}
var dataUrl = canvas.toDataURL('image/jpeg');
// 将这个图片插入到id为img1的img元素中
var img1 = document.getElementById('img1');
img1.src = dataUrl;
return dataUrl;
var image = new Image();
image.src = dataUrl;
return image;
// 将这个图片插入到id为img1的img元素中
//var img1 = document.getElementById('img1');
//img1.src = dataUrl;
}
export default createImage;