cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

Quick-add: + add another resource

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              #myCanvas {
  width: 100%;
  height: 100%;
}
body { 
  margin: 0px; 
  height: 100%;
  background-color: #000;
}
            
          
!
            
              paper.install(window);
paper.setup('myCanvas');
var canvasHeight = view.size.height;
var canvasWidth = view.size.width;
var speed1 = 1;
var speed2 = 10;
var moveX = 4;
var moveY = 2;
var forward = down = true;
var newX = (600 - (Math.random() * view.size.width)) / 50;
var newY = (200 - (Math.random() * view.size.height)) / 50;
var count = 0;

var carBody = new Path.Rectangle({
    point: [112, 250],
    size: [120, 50],
    fillColor: 'lightblue',
});

var carTop = new Path.Rectangle({
    point: [140, 220],
    size: [60, 30],
    fillColor: 'lightblue',
});

var carWindow = new Path.Rectangle({
    point: [145, 225],
    size: [50, 25],
    fillColor: 'white',
});

var wheel1 = new Path.Circle({
    center: new Point(205, 300),
    radius: 20,
    strokeColor: '#444',
    fillColor: 'grey',
    strokeWidth: 2
});

var wheel2 = wheel1.clone();
wheel2.position.x -= 65;

var head = new Path.Circle({
    center: new Point(180, 240),
    radius: 10,
    strokeColor: '#444',
    fillColor: 'pink',
    strokeWidth: 2
});

var car1 = new Layer({
	children: [carBody, carTop, carWindow, head, wheel1, wheel2]
});

var car2 = car1.clone();
car2.position.x += 100;
car2.position.y -= 120;

var car3 = car1.clone();
car3.position.x += 600;
car3.position.y += 200;

var car4 = car1.clone();
car4.position.x = 600;
car4.position.y = 200;

view.onFrame = function(event) {
	// car1 starts slow and accelerates
	car1.position.x += speed1;
	speed1 += 0.07;
	if (car1.position.x > canvasWidth + 50) {
		car1.position.x = -20;
		speed1 = 1;
   }
	
	// car2 starts fast and slows down
	car2.position.x += speed2;
	if (speed2 > 1.2) speed2 -= 0.03; // "if" prevents reversal on a wider screen
	if (car2.position.x > canvasWidth + 50) {
		car2.position.x = -20;
		speed2 = 10;
   }

	// car3 bounces around
	car3.position.x += moveX;
	car3.position.y += moveY;
	if (car3.position.x > canvasWidth - 50 && forward) {
		moveX = -4;
		forward = false;
	}
	if (car3.position.x < 50 && !forward) {
		moveX = 4;
		forward = true;
	}
	if (car3.position.y > canvasHeight - 50 && down) {
		moveY = -2;
		down = false;
	}
	if (car3.position.y < 50 && !down) {
		moveY = 2;
		down = true;
	}
	
	// car4 moves at random
	car4.position.x +=  newX;
	car4.position.y +=  newY;
	count++;
	if (count == 50) {
		newX = ((Math.random() * view.size.width) - car4.position.x) / 50;
	   newY = ((Math.random() * view.size.height) - car4.position.y) / 50;
		count = 0;
	}
};

            
          
!
999px
Loading ..................

Console