forked from skevy/wobble
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge JonDum/wobble
- Loading branch information
Showing
4 changed files
with
153 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<html> | ||
<head> | ||
<title>Squares</title> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> | ||
<style> | ||
* { | ||
margin: 0; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<div id="app"></div> | ||
<script src="./index.tsx"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
'use strict' | ||
|
||
import { Spring } from "../../dist/module" | ||
|
||
|
||
class Square { | ||
constructor(i, x, y) { | ||
this.i = i | ||
this.x = x | ||
this.y = y | ||
this.color = '#'+Math.random().toString(16).substr(2,6) | ||
|
||
// raf | ||
//this.springs = { | ||
//x: new Spring({fromValue: this.x}), // x | ||
//y: new Spring({fromValue: this.y}) // y | ||
//} | ||
|
||
// no raf | ||
this.springs = { | ||
x: new Spring({fromValue: this.x, raf: false}), // x | ||
y: new Spring({fromValue: this.y, raf: false}) // y | ||
} | ||
|
||
this.springs.x.onUpdate(s => this.x = s.currentValue) | ||
this.springs.y.onUpdate(s => this.y = s.currentValue) | ||
} | ||
setPosition(x, y) { | ||
// raf | ||
//this.springs.x.updateConfig({toValue: x}).start() | ||
//this.springs.y.updateConfig({toValue: y}).start() | ||
|
||
// no raf | ||
this.springs.x.setValue(x); | ||
this.springs.y.setValue(y); | ||
} | ||
tick(now) { | ||
this.springs.x._advanceSpringToTime(now, true) | ||
this.springs.y._advanceSpringToTime(now, true) | ||
//this.springs.x._step() | ||
//this.springs.x._step() | ||
} | ||
} | ||
|
||
class Renderer { | ||
constructor() { | ||
|
||
const COUNT = 1000 | ||
const canvas = document.createElement('canvas') | ||
const squares = [] | ||
|
||
canvas.style.width = canvas.style.height = '100%' | ||
document.body.appendChild(canvas) | ||
resizeCanvasToDisplaySize(canvas) | ||
|
||
for(var i = 0; i < COUNT; i++) { | ||
squares.push(new Square(i, Math.random() * canvas.width, Math.random() * canvas.height)) | ||
} | ||
|
||
this.squares = squares | ||
this.canvas = canvas | ||
this.ctx = canvas.getContext('2d') | ||
|
||
} | ||
|
||
draw = () => { | ||
const now = Date.now() | ||
const {canvas, squares, ctx} = this | ||
|
||
resizeCanvasToDisplaySize(canvas) | ||
|
||
const {width, height} = canvas | ||
|
||
ctx.clearRect(0, 0, width, height) | ||
|
||
const index = Math.floor(squares.length*Math.random()) | ||
squares[index].setPosition(Math.random() * canvas.width, Math.random() * canvas.height) | ||
|
||
for(var i = 0; i < squares.length; i++) { | ||
const square = squares[i] | ||
|
||
// with no raf, have to manually advance simulation. comment out if using raf | ||
square.tick(now) | ||
|
||
ctx.fillStyle = square.color | ||
ctx.fillRect(square.x, square.y, 10, 10) | ||
} | ||
|
||
requestAnimationFrame(this.draw) | ||
} | ||
|
||
} | ||
|
||
function resizeCanvasToDisplaySize(canvas) { | ||
let w = (canvas.clientWidth*devicePixelRatio) | 0 | ||
let h = (canvas.clientHeight*devicePixelRatio) | 0 | ||
if (canvas.width != w || canvas.height != h) { | ||
canvas.width = w | ||
canvas.height = h | ||
} | ||
} | ||
|
||
const renderer = new Renderer() | ||
renderer.draw() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters