Skip to content

Commit

Permalink
create ./examples/4/
Browse files Browse the repository at this point in the history
`clipImageSprite` no longer sets the `width` and `height`
only one `source_loaded` promise is ever made by `Sprite`
  • Loading branch information
omar-azmi committed Feb 8, 2023
1 parent 5ca7b1d commit 1236e81
Show file tree
Hide file tree
Showing 6 changed files with 321 additions and 6 deletions.
1 change: 1 addition & 0 deletions examples/4/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<script src="./index.ts" type="module"></script>
57 changes: 57 additions & 0 deletions examples/4/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { Sprite, JAtlasManager } from "../../src/mod.ts"

const
FPS = 60,
W = 800,
H = 600,
canvas = document.createElement("canvas")!,
ctx = canvas.getContext("2d")!,
sprites: Sprite[] = []

JAtlasManager.fromURL("./manuscript.jpg.jatlas.json")
.then(async (new_atlas_man) => {
await new_atlas_man.source_loaded
await new_atlas_man.entries_loaded
window.atlas_man = new_atlas_man
for (const id of Object.keys(new_atlas_man.entries)) {
const
new_sprite = new_atlas_man.getEntryImageSprite(parseFloat(id)),
scale = 0.33
new_sprite.source_loaded
.then((s) => {
const { width, height } = s.bitmap!
s.setConfig({
x: Math.random() * W,
y: Math.random() * H,
width: width * scale,
height: height * scale,
})
})
sprites.push(new_sprite)
}
})


document.body.appendChild(canvas)
canvas.width = W
canvas.height = H
let t = 0
const
drawAll = () => {
ctx.resetTransform()
ctx.fillStyle = "yellow"
ctx.fillRect(0, 0, W, H)
for (const sprite of sprites) {
sprite.coords[0] += 50 / FPS * Math.random()
sprite.coords[1] += 50 / FPS * Math.random()
if (sprite.coords[0] > W) sprite.coords[0] = 0
if (sprite.coords[1] > H) sprite.coords[1] = 0
sprite.draw(ctx)
}
t += 1 / FPS
},
RUN = setInterval(requestAnimationFrame, 1 / FPS, drawAll)

Object.assign(window, {
drawAll, RUN, canvas, ctx, sprites
})
Binary file added examples/4/manuscript.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 1236e81

Please sign in to comment.