This repository has been archived by the owner on Jul 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
73 lines (61 loc) · 2.09 KB
/
index.ts
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { Animator, Atlas, Cel } from '@/atlas-pack'
import atlasJSON from './atlas.json' assert { type: 'json' }
import { FilmID } from './film-id.ts'
interface Demo {
readonly window: Window
readonly canvas: HTMLCanvasElement
readonly context: CanvasRenderingContext2D
readonly backpacker: Animator
readonly frog: Animator
readonly sheet: HTMLImageElement
readonly atlas: Atlas<FilmID>
}
async function main(window: Window): Promise<void> {
console.log(
`
atlas-pack ┌>°┐
by │ │idoid
└──┘
`.trim(),
)
const canvas = window.document.getElementsByTagName('canvas').item(0)
if (canvas == null) throw Error('Canvas missing.')
const context = canvas.getContext('2d')
if (context == null) throw Error('Context missing.')
// Use nearest neighbor scaling.
context.imageSmoothingEnabled = false
const atlas = Atlas.fromJSON<FilmID>(atlasJSON)
const time = performance.now()
const demo = {
window,
canvas,
context,
backpacker: new Animator(atlas.filmByID['backpacker--WalkRight'], time),
frog: new Animator(atlas.filmByID['frog--EatLoop'], time),
sheet: await loadImage('atlas.png'),
atlas,
}
window.requestAnimationFrame((time) => loop(demo, time))
}
function loop(demo: Demo, time: number): void {
demo.context.clearRect(0, 0, demo.canvas.width, demo.canvas.height)
draw(demo, demo.backpacker.cel(time), 0, 0)
draw(demo, demo.frog.cel(time), 0, 128)
demo.window.requestAnimationFrame((time) => loop(demo, time))
}
function draw(demo: Demo, cel: Readonly<Cel>, x: number, y: number): void {
const scale = 16
const { bounds } = cel
const atlasSource = [bounds.x, bounds.y, bounds.w, bounds.h] as const
const canvasDest = [x, y, bounds.w * scale, bounds.h * scale] as const
demo.context.drawImage(demo.sheet, ...atlasSource, ...canvasDest)
}
function loadImage(uri: string): Promise<HTMLImageElement> {
return new Promise((resolve, reject) => {
const image = new Image()
image.onload = () => resolve(image)
image.onerror = () => reject(image)
image.src = uri
})
}
await main(window)