-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathsketch.js
executable file
·50 lines (40 loc) · 1.29 KB
/
sketch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
let blockA;
let blockB;
let ground;
let mouse;
let bg2;
let bg1;
let fg1;
function setup() {
const canvas = createCanvas(800, 600);
// create an engine
let engine = Matter.Engine.create();
let world = engine.world;
// create two boxes and a ground
blockA = new Block(world, { x: 400, y: 200, w: 80, h: 80, color: '#F6C780' });
blockB = new Block(world, { x: 470, y: 50, w: 160, h: 80, color: '#9DD9F2' });
ground = new Block(world, { x: 600, y: 500, w: 810, h: 15, color: 'grey' }, { isStatic: true, angle: PI/36 });
// run the engine
Matter.Runner.run(engine);
// add a mouse to manipulate Matter objects
mouse = new Mouse(engine, canvas, { stroke: 'magenta', strokeWeight: 2 });
// Select the elements from the HTML
bg2 = selectAll('#background_2');
bg1 = selectAll('#background_1');
fg1 = selectAll('#foreground_1');
}
function draw() {
const moveSpeed = -0.3;
const horizontalMove = mouseX * moveSpeed;
bg2[0].elt.style.transform = 'translateX(' + horizontalMove * 0.05 + 'px)';
bg1[0].elt.style.transform = 'translateX(' + horizontalMove * 0.5 + 'px)';
fg1[0].elt.style.transform = 'translateX(' + horizontalMove * 1.3 + 'px)';
clear();
push();
translate(horizontalMove, 0);
blockA.draw();
blockB.draw();
ground.draw();
mouse.draw();
pop();
}