html5黑板源码,HTML5/Canvas黑板涂鸦动画

JavaScript

语言:

JaveScriptBabelCoffeeScript

确定

class Pencil {

constructor( x, y ) {

this.position = { 'x': x, 'y': y }

this.radius = 1

this.color = getRandomColor()

}

}

class Experience {

constructor( container ) {

console.clear()

this.canvas = document.createElement( 'canvas' )

container.appendChild( this.canvas )

this.context = this.canvas.getContext( '2d' )

const fps = 120

this.fpsInterval = 1000 / fps

this.then = Date.now()

this.resize()

this.noise = new SimplexNoise( Math.random )

const pencilsNumber = 50

this.pencils = []

this.counter = 0

this.coef = 1

for( let i = 0; i < pencilsNumber; i++ ) {

const pencil = new Pencil( Math.random() * this.canvas.width, Math.random() * this.canvas.height )

this.pencils.push( pencil )

}

this.bind()

this.loop()

}

bind() {

window.addEventListener( 'resize', this.resize.bind( this ), false )

}

render() {

for( let pencil of this.pencils ) {

pencil.position.x += this.noise.noise3D( pencil.position.x / 100, pencil.position.x / 100, this.counter / 100 ) * this.coef

pencil.position.y += this.noise.noise3D( pencil.position.y / 100, pencil.position.y / 100, this.counter / 100 ) * this.coef

this.context.beginPath()

this.context.arc( pencil.position.x, pencil.position.y, pencil.radius, 0, Math.PI * 2, true )

this.context.fillStyle = pencil.color

this.context.fill()

this.context.closePath()

}

this.counter++

}

loop() {

this.raf = window.requestAnimationFrame( this.loop.bind( this ) )

const now = Date.now()

const delta = now - this.then

if( delta > this.fpsInterval ) {

this.render()

this.then = now

}

}

resize() {

this.canvas.width = window.innerWidth

this.canvas.height = window.innerHeight

this.center = {

'x': this.canvas.width / 2,

'y': this.canvas.height / 2

}

}

}

const getRandomColor = () => {

const value = Math.floor( Math.random() * 255 )

return '#' + value.toString( 16 ) + value.toString( 16 ) + value.toString( 16 )

}

const container = document.getElementById( 'canvas' )

let experience = new Experience( container )

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值