[英]HTML5 Canvas - Repeat Canvas Element as a Pattern
I have a 64x64 canvas square element which I want to repeat in x- and y-directions to fill the page. 我有一个64x64画布正方形元素,我想在x和y方向上重复以填充页面。 I've found many explanations on how to do this with an image, but none explaining how to do it with a canvas element.
我已经找到了很多关于如何对图像执行此操作的解释,但没有解释如何对canvas元素进行操作。 Here is my code so far:
到目前为止,这是我的代码:
$(document).ready(function(){
var canvas = document.getElementById('dkBg');
var ctx = canvas.getContext('2d');
ctx.canvas.width = window.innerWidth;
ctx.canvas.height = window.innerHeight;
ctx.fillStyle = 'rgb(0,0,0)';
//I want the following rectangle to be repeated:
ctx.fillRect(0,0,64,64);
for(var w=0; w<=64; w++){
for(var h=0; h<=64; h++){
rand = Math.floor(Math.random()*50);
while(rand<20){
rand = Math.floor(Math.random()*50);
}
opacity = Math.random();
while(opacity<0.5){
opacity = Math.random();
}
ctx.fillStyle= 'rgba('+rand+','+rand+','+rand+','+opacity+')';
ctx.fillRect(w,h,1,1);
}
}
});
The thing is, I don't want all of the random numbers/etc to be regenerated. 问题是,我不想重新生成所有的随机数/等。 I just want to tile the exact same square to fit the page.
我只想平铺完全相同的正方形以适合页面。 Is this possible?
这可能吗?
Here is a fiddle: http://jsfiddle.net/ecMDq/ 这是一个小提琴: http : //jsfiddle.net/ecMDq/
Doing what you want is actually super easy. 做您想做的事实际上非常容易。 You do not need to use images at all .
您根本不需要使用图像 。 The createPattern function accepts an image or another canvas!
createPattern函数接受图像或其他画布! (Or a video tag, even)
(甚至是视频标签)
All you have to do is make a canvas that is only 64x64 large and make the pattern on it. 您所要做的就是制作一个只有64x64大的画布,并在其上制作图案。 Let's call this canvas
pattern
. 让我们称之为画布
pattern
。 You only have to make your design once. 您只需要设计一次。
Then with the context of the main canvas we can do: 然后,在主画布的上下文中,我们可以执行以下操作:
// "pattern" is our 64x64 canvas, see code in fiddle
var pattern = ctx.createPattern(pattern, "repeat");
ctx.fillStyle = pattern;
Working example using your code to make the pattern, then repeating it onto a 500x500 canvas: 使用您的代码制作模式,然后将其重复到500x500画布上的工作示例:
You can get the base64 version of your image using the toDataURL() method of the canvas element. 您可以使用canvas元素的toDataURL()方法获取图像的base64版本。
From there it's as simple as setting the background-image of your page to the string "url(" + base64 + ")"
从那里开始,只需将页面的背景图像设置为字符串
"url(" + base64 + ")"
Here is a working example: http://jsfiddle.net/ecMDq/1/ 这是一个工作示例: http : //jsfiddle.net/ecMDq/1/
$(document).ready(function(){
var canvas = document.getElementById('dkBg');
var ctx = canvas.getContext('2d');
ctx.canvas.width = 64; //window.innerWidth;
ctx.canvas.height = 64; //window.innerHeight;
ctx.fillStyle = 'rgb(0,0,0)';
//I want the following rectangle to be repeated:
ctx.fillRect(0,0,64,64);
for(var w=0; w<=64; w++){
for(var h=0; h<=64; h++){
rand = Math.floor(Math.random()*50);
while(rand<20){
rand = Math.floor(Math.random()*50);
}
opacity = Math.random();
while(opacity<0.5){
opacity = Math.random();
}
ctx.fillStyle= 'rgba('+rand+','+rand+','+rand+','+opacity+')';
ctx.fillRect(w,h,1,1);
}
}
document.documentElement.style.backgroundImage =
'url(' +canvas.toDataURL() + ')';
});
Note that you need to make the canvas 64x64 because that's the size of your source image. 请注意,您需要将画布制作为64x64,因为这是源图像的大小。 You can also now make the canvas
display:none
, or even remove it from the dom completely because it's only acting as a source for the background-image. 现在,您还可以使canvas
display:none
,甚至可以将其从dom中完全删除,因为它仅用作背景图像的来源。
Also, what on earth is up with those while loops? 另外,那些while循环到底是怎么回事?
while(rand<20){
rand = Math.floor(Math.random()*50);
}
It looks like you're trying to enforce a minimum value. 看来您要强制执行最小值。 Just use this:
只需使用此:
rand = Math.floor(Math.random() * (50-20) + 20);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.