HTML5如何画有颜色的矩阵,HTML5/Canvas具有吸引力的点矩阵

JavaScript

语言:

JaveScriptBabelCoffeeScript

确定

document.addEventListener('DOMContentLoaded', init, false)

let APP

function init() {

APP = new App()

events()

loop()

}

function loop() {

APP.render()

requestAnimationFrame(loop)

}

function events() {

document.addEventListener('mousemove', APP.mousemoveHandler, false)

document.addEventListener('mouseleave', APP.mouseleaveHandler, false)

window.addEventListener('resize', APP.resize, false)

}

class App {

constructor() {

this.canvas = document.getElementById('playground')

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

this.canvas.width = this.width = window.innerWidth

this.canvas.height = this.height = window.innerHeight

this.setupDots()

this.resize = this.resize.bind(this)

this.mousemoveHandler = this.mousemoveHandler.bind(this)

this.mouseleaveHandler = this.mouseleaveHandler.bind(this)

}

setupDots() {

this.dots = []

this.scl = 60

this.cols = this.width / this.scl

this.rows = this.height / this.scl

let id = 0

for (let x = 0; x < this.cols; x += 1) {

for (let y = 0; y < this.rows; y += 1) {

this.dots.push(new Dot(id, x * this.scl, y * this.scl, this.context, this.scl))

id += 1

}

}

}

resize() {

this.canvas.width = this.width = window.innerWidth

this.canvas.height = this.height = window.innerHeight

this.setupDots()

}

mousemoveHandler(event) {

this.dots.forEach(d => {

d.mousemove(event)

})

}

mouseleaveHandler() {

this.dots.forEach(d => {

d.isHover = false

})

}

render() {

this.context.clearRect(0, 0, this.width, this.height)

this.dots.forEach(d => {

d.render()

})

}

}

class Dot {

constructor(id, x, y, context, scl) {

this.id = id

this.x = x

this.y = y

this.new = {

x: x,

y: y

}

this.radius = 3

this.context = context

this.scl = scl

this.isHover = false

this.isANimated = false

}

mousemove(event) {

const x = event.clientX

const y = event.clientY

this.isHover = ((Math.abs(this.x - x) < (this.scl / 4 * 3)) && (Math.abs(this.y - y) < (this.scl / 4 * 3))) ? true : false

if (this.isHover) {

TweenMax.to(

this.new,

0.4, {

x: x,

y: y

}

)

} else {

TweenMax.to(

this.new,

0.4, {

x: this.x,

y: this.y

}

)

}

}

render() {

this.context.beginPath()

this.context.arc(this.new.x, this.new.y, this.radius, 0, 2 * Math.PI, false)

this.context.fillStyle = 'rgba(90, 251, 134, 1)'

this.context.globalAlpha = (this.isHover) ? 1 : 0.25

this.context.fill()

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值