开发者

Repeat shapes with boundary detection - html5, canvas, javascript

开发者 https://www.devze.com 2023-01-31 21:30 出处:网络
I am wanting to create a simple abstract pattern using the html5 canvas tag and javascript. I have worked out the guts of what I want it to do using some variables, functions and objects, but with the

I am wanting to create a simple abstract pattern using the html5 canvas tag and javascript. I have worked out the guts of what I want it to do using some variables, functions and objects, but with the boundary detection that I have employed I am wanting each particular shape to go back to its starting position when it goes out of the screen (and thus loop the animation).

So with that being my question, here is my code. Also any other structure tips are appreciated as I am new to OO in Javascript.

开发者_如何学JAVA

See my progress here: http://helloauan.com/apps/test/

Cheers!


I'm not really sure if what you mean exactly is, once the big white diagnal lines are all the way off the top right corner of page, that's when you want them to start back at the bottom left ? right?

What you need to do is check if the line is beyond the width and height of the canvas, and in your case, the window itself since the canvas fills the browser window. So you need to do a series of conditionals. You check if the line x + line width is > canvas width and line.y + line height is > canvas height. If both are true then set the x and y of the line to - what it is at that time. So something like:

if( line.x + line.width > canvas.width && line.y + line.height < 0) {
  line.x = -0;
  line.y = canvasHeight + line.height;
} 

This is how I recycle circles that come in from the right side of the screen and once they exit the left side they start over on the right.

if( d.x + d.radius < 0 ) {
    d.radius = 5+(Math.random()*10);
    d.x = cwidth + d.radius;
    d.y = Math.floor(Math.random()*cheight);
    d.vX = -5-(Math.random()*5);
}

The first thing is just psuedo, you should take a look at a thing I made to use as a starting point for things like this. The structure of your code could use some more organization, canvas gets real complex real quick. Using the arrow keys, move the square off any one of the 4 sides and see it come in on opposite side. http://anti-code.com/games/envy/envy.html

Fork if you want: https://github.com/jaredwilli/envy

0

精彩评论

暂无评论...
验证码 换一张
取 消