Skip to content

Commit 5b71d80

Browse files
committed
examples: fix resolution of graphs in examples/06_array_interpolation
1 parent df82146 commit 5b71d80

File tree

2 files changed

+47
-29
lines changed

2 files changed

+47
-29
lines changed

examples/06_array_interpolation.html

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ <h2>06 _ array interpolation</h2>
2525
<div id="target"></div>
2626

2727
<script src="../dist/tween.umd.js"></script>
28+
<script src="js/createGraph.js"></script>
2829
<script>
2930
init()
3031
animate()
@@ -73,51 +74,70 @@ <h2>06 _ array interpolation</h2>
7374
target.appendChild(createPath('', TWEEN.Interpolation.Bezier))
7475
target.appendChild(createPath('', TWEEN.Interpolation.CatmullRom))
7576

76-
function createPath(title, interpolation) {
77+
function createPath(text, interpolation, width = 240, height = 160) {
7778
var div = document.createElement('div')
7879
div.style.display = 'inline-block'
80+
// +20 for padding
7981
div.style.width = width + 20 + 'px'
8082
div.style.height = height + 20 + 'px'
8183

8284
var canvas = document.createElement('canvas')
83-
canvas.width = width
84-
canvas.height = height
85+
canvas.style.width = width + 'px'
86+
canvas.style.height = height + 'px'
87+
canvas.width = toPhysicalPx(width)
88+
canvas.height = toPhysicalPx(height)
8589

8690
var context = canvas.getContext('2d')
8791
context.fillStyle = 'rgb(250,250,250)'
88-
context.fillRect(0, 0, width, height)
92+
context.fillRect(0, 0, toPhysicalPx(width), toPhysicalPx(height))
8993

94+
context.lineWidth = toPhysicalPx(1)
95+
context.strokeStyle = 'rgb(230,230,230)'
96+
97+
// points
9098
context.fillStyle = 'rgb(200,200,200)'
91-
context.fillRect(x0 - 3, y0 - 3, 6, 6)
92-
context.fillRect(xA[xA.length - 1] - 3, yA[yA.length - 1] - 3, 6, 6)
99+
context.fillRect(
100+
toPhysicalPx(x0) - toPhysicalPx(3),
101+
toPhysicalPx(y0) - toPhysicalPx(3),
102+
toPhysicalPx(6),
103+
toPhysicalPx(6),
104+
)
105+
context.fillRect(
106+
toPhysicalPx(xA[xA.length - 1]) - toPhysicalPx(3),
107+
toPhysicalPx(yA[yA.length - 1]) - toPhysicalPx(3),
108+
toPhysicalPx(6),
109+
toPhysicalPx(6),
110+
)
93111

94112
for (var i = 0; i < xA.length; i++) {
95-
context.fillRect(xA[i] - 2, yA[i] - 2, 4, 4)
113+
context.fillRect(
114+
toPhysicalPx(xA[i]) - toPhysicalPx(2),
115+
toPhysicalPx(yA[i]) - toPhysicalPx(2),
116+
toPhysicalPx(4),
117+
toPhysicalPx(4),
118+
)
96119
}
120+
//
97121

98-
context.lineWidth = 2
122+
context.lineWidth = toPhysicalPx(2)
99123
context.strokeStyle = 'rgba(255,127,127,0.9)'
124+
context.beginPath()
125+
context.moveTo(toPhysicalPx(x0), toPhysicalPx(y0))
126+
context.lineCap = 'round'
100127

101-
var obj = {x: x0, y: y0, old: {x: x0, y: y0}}
128+
var position = {x: x0, y: y0}
102129

103-
new TWEEN.Tween(obj)
130+
new TWEEN.Tween(position)
104131
.to({x: xA, y: yA}, 3000)
105-
.onUpdate(function (object) {
106-
context.beginPath()
107-
context.moveTo(object.old.x, object.old.y)
108-
context.lineTo(object.x, object.y)
109-
context.closePath()
132+
.easing(TWEEN.Easing.Linear.None)
133+
.interpolation(interpolation)
134+
.onUpdate(function () {
135+
context.lineTo(toPhysicalPx(position.x), toPhysicalPx(position.y))
110136
context.stroke()
111-
112-
object.old.x = object.x
113-
object.old.y = object.y
114137
})
115-
.interpolation(interpolation)
116-
.easing(TWEEN.Easing.Linear.None)
117-
.delay(250)
118138
.start()
119139

120-
div.appendChild(document.createTextNode(title))
140+
div.appendChild(document.createTextNode(text))
121141
div.appendChild(document.createElement('br'))
122142
div.appendChild(canvas)
123143

examples/js/createGraph.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
function createGraph(t, f, c) {
2-
const width = 180
3-
const height = 100
4-
const toPhysicalPx = cssPx => cssPx * devicePixelRatio
1+
const toPhysicalPx = cssPx => cssPx * devicePixelRatio
52

3+
function createGraph(text, easingFn, width = 180, height = 100) {
64
var div = document.createElement('div')
75
div.style.display = 'inline-block'
86
// +20 for padding
@@ -31,7 +29,7 @@ function createGraph(t, f, c) {
3129
context.stroke()
3230

3331
context.lineWidth = toPhysicalPx(2)
34-
context.strokeStyle = 'rgb(255,127,127)'
32+
context.strokeStyle = 'rgba(255,127,127,0.9)'
3533
context.beginPath()
3634
context.moveTo(toPhysicalPx(5), toPhysicalPx(80))
3735
context.lineCap = 'round'
@@ -44,14 +42,14 @@ function createGraph(t, f, c) {
4442
.start()
4543
new TWEEN.Tween(position)
4644
.to({y: toPhysicalPx(20)}, 2000)
47-
.easing(f)
45+
.easing(easingFn)
4846
.onUpdate(function () {
4947
context.lineTo(position.x, position.y)
5048
context.stroke()
5149
})
5250
.start()
5351

54-
div.appendChild(document.createTextNode(t))
52+
div.appendChild(document.createTextNode(text))
5553
div.appendChild(document.createElement('br'))
5654
div.appendChild(canvas)
5755

0 commit comments

Comments
 (0)