Skip to content

Commit c48e55b

Browse files
committed
fix: modernize the code in the examples
- convert 'var' → 'const' (or 'let') - use arrow functions for callbacks - use more common indentations patterns - use querySelector more consistantly across examples closes #118
1 parent d993fa9 commit c48e55b

File tree

12 files changed

+79
-81
lines changed

12 files changed

+79
-81
lines changed

examples/animated-gif/index.html

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
<!-- use the picture element to use a more
2-
modern animated image format, if supported,
3-
and fall back to .gif if not -->
1+
<!--
2+
The picture element enables browsers to use a more modern
3+
animated image format if supported, and fall back to .gif
4+
if not. The formats are listed in ascending filesize order,
5+
because browsers use the first one they support.
6+
-->
47
<picture>
5-
<!-- these formats are listed in ascending filesize order, because browsers use the first one they support -->
6-
<source srcset="image.mp4" type="video/mp4"> <!-- Safari 11.1+ -->
7-
<source srcset="image.apng" type="image/apng"> <!-- Firefox -->
8-
<source srcset="image.webp" type="image/webp"> <!-- Firefox, Blink-based browsers -->
9-
<img src="image.gif" alt="Bouncy ball"> <!-- everything else -->
8+
<source srcset="image.mp4" type="video/mp4">
9+
<source srcset="image.apng" type="image/apng">
10+
<source srcset="image.webp" type="image/webp">
11+
<img src="image.gif" alt="Bouncy ball">
1012
</picture>

examples/canvas/script.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
var h = 575; // x vertex, half of total bounce duration
2-
var k = 160; // total bounce height
3-
var a = 4 * k / Math.pow(h * 2, 2); // coefficient: 0.000483932
4-
var ballColor = getComputedStyle(document.documentElement).getPropertyValue('--canvas');
5-
var ypos, start, time, timestamp;
1+
const h = 575; // x vertex, half of total bounce duration
2+
const k = 160; // total bounce height
3+
const a = 4 * k / Math.pow(h * 2, 2); // coefficient: 0.000483932
4+
const ballColor = getComputedStyle(document.documentElement).getPropertyValue('--canvas');
5+
let ypos, start, time, timestamp;
66

77
// Canvas setup
8-
var canvasEl = document.querySelector('canvas');
9-
var offset = canvasEl.width / 2;
10-
var ctx = canvasEl.getContext('2d');
8+
const canvasEl = document.querySelector('canvas');
9+
const offset = canvasEl.width / 2;
10+
const ctx = canvasEl.getContext('2d');
1111
ctx.fillStyle = ballColor;
1212

1313
// Animation Loop

examples/d3/script.js

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
1-
var svg, ball;
2-
var ballColor = getComputedStyle(document.documentElement).getPropertyValue('--d3');
1+
const ballColor = getComputedStyle(document.documentElement).getPropertyValue('--d3');
2+
const svg = d3.select('body')
3+
.append('svg')
4+
.attr('width', 50)
5+
.attr('height', 210)
6+
.append('g')
7+
.attr('transform', 'translate(25,25)');
38

4-
svg = d3.select('body')
5-
.append('svg')
6-
.attr('width', 50)
7-
.attr('height', 210)
8-
.append('g')
9-
.attr('transform', 'translate(25,25)');
10-
11-
ball = svg.append('circle')
12-
.attr('r', 25)
13-
.attr('fill', ballColor);
9+
const ball = svg.append('circle')
10+
.attr('r', 25)
11+
.attr('fill', ballColor);
1412

1513
function bounce() {
16-
ball
17-
.transition()
14+
ball.transition()
1815
.duration(575)
1916
.ease(d3.easeQuadOut)
2017
.attr('cy', 0)

examples/jquery/script.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
var $ball = $('ball'),
2-
DURATION = 575;
1+
const DURATION = 575;
2+
const $ball = $('ball');
33

44
function dropBall() {
55
// We animate 'top' because jquery can't
@@ -12,6 +12,6 @@ function restoreBall() {
1212
$ball.animate({ top: '0' }, DURATION, 'easeOutQuad', dropBall);
1313
}
1414

15-
$(document).ready(function(){
15+
$(document).ready(() => {
1616
dropBall();
1717
});

examples/matter-js/script.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1+
const ballColor = getComputedStyle(document.documentElement).getPropertyValue('--matterjs');
12
const Engine = Matter.Engine,
23
Render = Matter.Render,
34
Runner = Matter.Runner,
45
Bodies = Matter.Bodies,
56
World = Matter.World;
67

7-
const runner = Runner.create({
8-
isFixed: true,
9-
delta: 1000 / 120,
10-
});
118
const engine = Engine.create();
129
const render = Render.create({
1310
element: document.body,
@@ -19,8 +16,10 @@ const render = Render.create({
1916
background: '#111'
2017
}
2118
});
22-
const ballColor = getComputedStyle(document.documentElement).getPropertyValue('--matterjs');
23-
19+
const runner = Runner.create({
20+
isFixed: true,
21+
delta: 1000 / 120,
22+
});
2423

2524
// Use a many-sided polygon as a ball, to ensure 100% elasticity.
2625
// See https://github.com/liabru/matter-js/issues/256

examples/mojs/script.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
var ball = new mojs.Shape({
2-
shape: 'circle',
3-
radius: 25,
4-
top: 33,
5-
left: 33,
6-
y: { 0 : 160 },
1+
const ball = new mojs.Shape({
2+
shape: 'circle',
3+
radius: 25,
4+
top: 33,
5+
left: 33,
6+
y: { 0: 160 },
77
duration: 575,
8-
repeat: 1,
9-
easing: 'quad.in',
10-
isYoyo: true,
11-
onComplete: function() { ball.replay(); }
8+
repeat: 1,
9+
easing: 'quad.in',
10+
isYoyo: true,
11+
onComplete: () => ball.replay()
1212
}).play();

examples/motion-one/script.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const ball = document.getElementsByTagName("ball");
1+
const ball = document.querySelector("ball");
22

33
Motion.animate(
44
ball,

examples/p5/script.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
var h = 575; // x vertex, half of total bounce duration
2-
var k = 160; // y vertex, total bounce height
3-
var a = -4 * k / Math.pow(h * 2, 2); // coefficient: -.000483932
4-
var ypos, start, time, timestamp;
5-
var ballColor = getComputedStyle(document.documentElement).getPropertyValue('--p5');
1+
const h = 575; // x vertex, half of total bounce duration
2+
const k = 160; // y vertex, total bounce height
3+
const a = -4 * k / Math.pow(h * 2, 2); // coefficient: -.000483932
4+
const ballColor = getComputedStyle(document.documentElement).getPropertyValue('--p5');
5+
let ypos, start, time, timestamp;
66

77
function setup() {
88
createCanvas(66, 226);

examples/vanilla-js/script.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
var ball = document.querySelector('ball');
2-
var h = 575; // x vertex, half of total bounce duration
3-
var k = 160; // y vertex, total bounce height
4-
var a = -4 * k / Math.pow(h * 2, 2); // coefficient: -.000483932
5-
var ypos, start, time;
1+
const ball = document.querySelector('ball');
2+
const h = 575; // x vertex, half of total bounce duration
3+
const k = 160; // y vertex, total bounce height
4+
const a = -4 * k / Math.pow(h * 2, 2); // coefficient: -.000483932
5+
let ypos, start, time;
66

77
(function drawPosition(timestamp) {
88
if (!start) { start = timestamp };

examples/velocity/script.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const ball = document.getElementsByTagName('ball');
1+
const ball = document.querySelector('ball');
22

33
ball.velocity({
44
transform: [

0 commit comments

Comments
 (0)