33 lines
1.3 KiB
TypeScript
33 lines
1.3 KiB
TypeScript
import { Animator } from './Animator'
|
|
import { createSystem, SystemSettings } from '../system/snowSystem'
|
|
|
|
export function runSnow(canvas: HTMLCanvasElement, settings: SystemSettings){
|
|
const context = canvas.getContext('2d') as CanvasRenderingContext2D
|
|
let viewportWidth = window.innerWidth
|
|
let viewportHeight = window.innerHeight
|
|
const dpr = window.devicePixelRatio || 1
|
|
const system = createSystem(context, () => viewportWidth, () => viewportHeight, settings)
|
|
function resize(){
|
|
viewportWidth = window.innerWidth
|
|
viewportHeight = window.innerHeight
|
|
canvas.style.width = `${viewportWidth}px`
|
|
canvas.style.height = `${viewportHeight}px`
|
|
canvas.width = Math.floor(viewportWidth * dpr)
|
|
canvas.height = Math.floor(viewportHeight * dpr)
|
|
context.setTransform(dpr, 0, 0, dpr, 0, 0)
|
|
system.setViewport(viewportWidth, viewportHeight)
|
|
system.recomputeTarget()
|
|
}
|
|
resize()
|
|
const animator = new Animator(
|
|
(dt) => { system.updateSystem(dt); system.renderSystem() },
|
|
() => system.shouldStop(),
|
|
() => { context.clearRect(0, 0, viewportWidth, viewportHeight); canvas.style.display = 'none' }
|
|
)
|
|
function onResize(){ resize() }
|
|
window.addEventListener('resize', onResize)
|
|
animator.init()
|
|
animator.update()
|
|
return { stop(){ animator.stop(); window.removeEventListener('resize', onResize) } }
|
|
}
|