使用ResizeObserver代替window.resize事件

master
toesbieya 6 years ago
parent 998543eda0
commit 39fdbafab8
  1. 9
      vue/src/component/SearchForm/index.vue
  2. 19
      vue/src/mixin/chart/resize.js
  3. 9
      vue/src/plugin/canvasAnimation/firework/index.js
  4. 9
      vue/src/plugin/canvasAnimation/godrays/index.js
  5. 9
      vue/src/plugin/canvasAnimation/moveFollowMouse/index.js
  6. 9
      vue/src/plugin/canvasAnimation/particleBall/index.js
  7. 9
      vue/src/plugin/canvasAnimation/particleNetwork/index.js
  8. 9
      vue/src/plugin/canvasAnimation/particleWave/index.js
  9. 9
      vue/src/plugin/canvasAnimation/reflectRain/index.js
  10. 9
      vue/src/plugin/canvasAnimation/sparkRain/index.js
  11. 9
      vue/src/plugin/canvasAnimation/sunlight/index.js
  12. 12
      vue/src/view/example/cool/fluidPage.vue

@ -51,11 +51,14 @@ export default {
mounted() { mounted() {
this.resize() this.resize()
this.resizeObserver = new ResizeObserver(() => this.resize())
window.addEventListener('resize', this.resize) this.resizeObserver.observe(this.$el.parentNode)
this.$once('hook:beforeDestroy', () => { this.$once('hook:beforeDestroy', () => {
window.removeEventListener('resize', this.resize) if (this.resizeObserver) {
this.resizeObserver.disconnect()
this.resizeObserver = null
}
}) })
}, },

@ -1,32 +1,29 @@
import {debounce} from '@/util' import {debounce} from '@/util'
export default { export default {
data() {
return {
$_sidebarElm: null,
$_resizeHandler: null
}
},
methods: { methods: {
$_initResizeEvent() { $_initResizeEvent() {
window.addEventListener('resize', this.$_resizeHandler) this.resizeObserver = new ResizeObserver(() => this.$_resizeHandler())
this.resizeObserver.observe(this.$el)
}, },
$_destroyResizeEvent() { $_destroyResizeEvent() {
window.removeEventListener('resize', this.$_resizeHandler) if (this.resizeObserver) {
this.resizeObserver.disconnect()
this.resizeObserver = null
}
} }
}, },
mounted() { mounted() {
this.$_resizeHandler = debounce(() => this.chart && this.chart.resize()) this.$_resizeHandler = debounce(() => this.chart && this.chart.resize())
this.$_initResizeEvent() this.$_initResizeEvent()
this.$nextTick(this.$_resizeHandler) this.$nextTick(() => this.$_resizeHandler())
this.$once('hook:deactivated', this.$_destroyResizeEvent) this.$once('hook:deactivated', this.$_destroyResizeEvent)
this.$once('hook:beforeDestroy', this.$_destroyResizeEvent) this.$once('hook:beforeDestroy', this.$_destroyResizeEvent)
}, },
activated() { activated() {
this.$_initResizeEvent() this.$_initResizeEvent()
this.$nextTick(this.$_resizeHandler) this.$nextTick(() => this.$_resizeHandler())
} }
} }

@ -73,14 +73,14 @@ export default class Firework {
this.rAF = null this.rAF = null
this.timer = null this.timer = null
this.stopSign = false this.stopSign = false
this.resize = this.resize.bind(this)
this.loop = this.loop.bind(this) this.loop = this.loop.bind(this)
this.addRocket = this.addRocket.bind(this) this.addRocket = this.addRocket.bind(this)
this.start() this.start()
} }
start() { start() {
window.addEventListener('resize', this.resize) this.resizeObserver = new ResizeObserver(() => this.resize())
this.resizeObserver.observe(this.canvas)
document.addEventListener('click', this.addRocket) document.addEventListener('click', this.addRocket)
this.timer = window.setInterval(this.addRocket, 1000) this.timer = window.setInterval(this.addRocket, 1000)
this.resize() this.resize()
@ -88,7 +88,10 @@ export default class Firework {
} }
stop() { stop() {
window.removeEventListener('resize', this.resize) if (this.resizeObserver) {
this.resizeObserver.disconnect()
this.resizeObserver = null
}
window.removeEventListener('click', this.addRocket) window.removeEventListener('click', this.addRocket)
this.stopSign = true this.stopSign = true
this.rockets = [] this.rockets = []

@ -12,19 +12,22 @@ export default class Godrays {
this.godraysCtx = this.godraysCanvas.getContext('2d') this.godraysCtx = this.godraysCanvas.getContext('2d')
this.rAF = null this.rAF = null
this.stopSign = false this.stopSign = false
this.resize = this.resize.bind(this)
this.loop = this.loop.bind(this) this.loop = this.loop.bind(this)
this.start() this.start()
} }
start() { start() {
window.addEventListener('resize', this.resize) this.resizeObserver = new ResizeObserver(() => this.resize())
this.resizeObserver.observe(this.canvas)
this.resize() this.resize()
this.loop() this.loop()
} }
stop() { stop() {
window.removeEventListener('resize', this.resize) if (this.resizeObserver) {
this.resizeObserver.disconnect()
this.resizeObserver = null
}
this.stopSign = true this.stopSign = true
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height) this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height)
this.godraysCtx.clearRect(0, 0, this.godraysCanvas.width, this.godraysCanvas.height) this.godraysCtx.clearRect(0, 0, this.godraysCanvas.width, this.godraysCanvas.height)

@ -10,7 +10,6 @@ export default class MoveFollowMouse {
this.num = num this.num = num
this.gravityDistance = gravityDistance this.gravityDistance = gravityDistance
this.mousePos = {x: 0, y: 0} this.mousePos = {x: 0, y: 0}
this.resize = this.resize.bind(this)
this.loop = this.loop.bind(this) this.loop = this.loop.bind(this)
this.getMousePos = this.getMousePos.bind(this) this.getMousePos = this.getMousePos.bind(this)
this.start() this.start()
@ -20,14 +19,18 @@ export default class MoveFollowMouse {
for (let i = 0; i < this.num; i += 1) { for (let i = 0; i < this.num; i += 1) {
this.particles.push(new Particle()) this.particles.push(new Particle())
} }
window.addEventListener('resize', this.resize) this.resizeObserver = new ResizeObserver(() => this.resize())
this.resizeObserver.observe(this.canvas)
window.addEventListener('mousemove', this.getMousePos) window.addEventListener('mousemove', this.getMousePos)
this.resize() this.resize()
this.loop() this.loop()
} }
stop() { stop() {
window.removeEventListener('resize', this.resize) if (this.resizeObserver) {
this.resizeObserver.disconnect()
this.resizeObserver = null
}
window.removeEventListener('mousemove', this.getMousePos) window.removeEventListener('mousemove', this.getMousePos)
this.stopSign = true this.stopSign = true
this.particles = [] this.particles = []

@ -11,20 +11,23 @@ export default class ParticleBall {
this.maxParticle = maxParticle this.maxParticle = maxParticle
this.turnSpeed = turnSpeed this.turnSpeed = turnSpeed
this.particles = [] this.particles = []
this.resize = this.resize.bind(this)
this.loop = this.loop.bind(this) this.loop = this.loop.bind(this)
this.start() this.start()
} }
start() { start() {
window.addEventListener('resize', this.resize) this.resizeObserver = new ResizeObserver(() => this.resize())
this.resizeObserver.observe(this.canvas)
this.resize() this.resize()
this.initDot() this.initDot()
this.loop() this.loop()
} }
stop() { stop() {
window.removeEventListener('resize', this.resize) if (this.resizeObserver) {
this.resizeObserver.disconnect()
this.resizeObserver = null
}
this.stopSign = true this.stopSign = true
this.particles = [] this.particles = []
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height) this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height)

@ -14,7 +14,6 @@ export default class ParticleNetwork {
this.rAF = null this.rAF = null
this.timer = null this.timer = null
this.stopSign = false this.stopSign = false
this.resize = this.resize.bind(this)
this.loop = this.loop.bind(this) this.loop = this.loop.bind(this)
this.start() this.start()
} }
@ -174,7 +173,8 @@ export default class ParticleNetwork {
this.removeInteractionParticle() this.removeInteractionParticle()
}.bind(this) }.bind(this)
window.addEventListener('resize', this.resize) this.resizeObserver = new ResizeObserver(() => this.resize())
this.resizeObserver.observe(this.canvas)
window.addEventListener('mousemove', this.onMouseMove) window.addEventListener('mousemove', this.onMouseMove)
window.addEventListener('touchmove', this.onTouchMove) window.addEventListener('touchmove', this.onTouchMove)
window.addEventListener('mousedown', this.onMouseDown) window.addEventListener('mousedown', this.onMouseDown)
@ -185,7 +185,10 @@ export default class ParticleNetwork {
} }
unbindUiActions() { unbindUiActions() {
window.removeEventListener('resize', this.resize) if (this.resizeObserver) {
this.resizeObserver.disconnect()
this.resizeObserver = null
}
window.removeEventListener('mousemove', this.onMouseMove) window.removeEventListener('mousemove', this.onMouseMove)
window.removeEventListener('touchmove', this.onTouchMove) window.removeEventListener('touchmove', this.onTouchMove)
window.removeEventListener('mousedown', this.onMouseDown) window.removeEventListener('mousedown', this.onMouseDown)

@ -20,7 +20,6 @@ export default class ParticleWave {
this.particleWaveWalker = 0 this.particleWaveWalker = 0
this.rAF = null this.rAF = null
this.stopSign = false this.stopSign = false
this.resize = this.resize.bind(this)
this.loop = this.loop.bind(this) this.loop = this.loop.bind(this)
this.start() this.start()
} }
@ -67,7 +66,8 @@ export default class ParticleWave {
} }
start() { start() {
window.addEventListener('resize', this.resize) this.resizeObserver = new ResizeObserver(() => this.resize())
this.resizeObserver.observe(this.canvas)
this.resize() this.resize()
this.initParticle() this.initParticle()
this.initParticleColor() this.initParticleColor()
@ -77,7 +77,10 @@ export default class ParticleWave {
} }
stop() { stop() {
window.removeEventListener('resize', this.resize) if (this.resizeObserver) {
this.resizeObserver.disconnect()
this.resizeObserver = null
}
this.stopSign = true this.stopSign = true
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height) this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height)
} }

@ -9,19 +9,22 @@ export default class ReflectRain {
this.stopSign = false this.stopSign = false
this.canvas = canvas this.canvas = canvas
this.ctx = canvas.getContext('2d') this.ctx = canvas.getContext('2d')
this.resize = this.resize.bind(this)
this.loop = this.loop.bind(this) this.loop = this.loop.bind(this)
this.start() this.start()
} }
start() { start() {
window.addEventListener('resize', this.resize) this.resizeObserver = new ResizeObserver(() => this.resize())
this.resizeObserver.observe(this.canvas)
this.resize() this.resize()
this.loop() this.loop()
} }
stop() { stop() {
window.removeEventListener('resize', this.resize) if (this.resizeObserver) {
this.resizeObserver.disconnect()
this.resizeObserver = null
}
this.stopSign = true this.stopSign = true
this.rainDrops = [] this.rainDrops = []
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height) this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height)

@ -24,14 +24,14 @@ export default class SparkRain {
this.rAF = null this.rAF = null
this.timer = null this.timer = null
this.stopSign = false this.stopSign = false
this.resize = this.resize.bind(this)
this.loop = this.loop.bind(this) this.loop = this.loop.bind(this)
this.addSpark = this.addSpark.bind(this) this.addSpark = this.addSpark.bind(this)
this.start() this.start()
} }
start() { start() {
window.addEventListener('resize', this.resize) this.resizeObserver = new ResizeObserver(() => this.resize())
this.resizeObserver.observe(this.canvas)
this.resize() this.resize()
this.timer = window.setInterval( this.timer = window.setInterval(
() => this.sparks.length < this.options.amount && this.addSpark(), () => this.sparks.length < this.options.amount && this.addSpark(),
@ -40,7 +40,10 @@ export default class SparkRain {
} }
stop() { stop() {
window.removeEventListener('resize', this.resize) if (this.resizeObserver) {
this.resizeObserver.disconnect()
this.resizeObserver = null
}
this.stopSign = true this.stopSign = true
this.sparks = [] this.sparks = []
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height) this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height)

@ -8,7 +8,6 @@ export default class Sunlight {
this.mouse = {down: 0} this.mouse = {down: 0}
this.rAF = null this.rAF = null
this.stopSign = false this.stopSign = false
this.resize = this.resize.bind(this)
this.loop = this.loop.bind(this) this.loop = this.loop.bind(this)
this.mousedown = this.mousedown.bind(this) this.mousedown = this.mousedown.bind(this)
this.mouseup = this.mouseup.bind(this) this.mouseup = this.mouseup.bind(this)
@ -16,7 +15,8 @@ export default class Sunlight {
} }
start() { start() {
window.addEventListener('resize', this.resize) this.resizeObserver = new ResizeObserver(() => this.resize())
this.resizeObserver.observe(this.canvas)
window.addEventListener('mouseup', this.mouseup) window.addEventListener('mouseup', this.mouseup)
window.addEventListener('mousedown', this.mousedown) window.addEventListener('mousedown', this.mousedown)
this.resize() this.resize()
@ -24,7 +24,10 @@ export default class Sunlight {
} }
stop() { stop() {
window.removeEventListener('resize', this.resize) if (this.resizeObserver) {
this.resizeObserver.disconnect()
this.resizeObserver = null
}
window.removeEventListener('mouseup', this.mouseup) window.removeEventListener('mouseup', this.mouseup)
window.removeEventListener('mousedown', this.mousedown) window.removeEventListener('mousedown', this.mousedown)
this.stopSign = true this.stopSign = true

@ -14,18 +14,20 @@ export default {
return { return {
sign: true, sign: true,
width: 1200, width: 1200,
height: 800, height: 800
$_resizeHandler: null,
parentDom: null
} }
}, },
methods: { methods: {
$_initResizeEvent() { $_initResizeEvent() {
window.addEventListener('resize', this.$_resizeHandler) this.resizeObserver = new ResizeObserver(() => this.$_resizeHandler())
this.resizeObserver.observe(this.parentDom)
}, },
$_destroyResizeEvent() { $_destroyResizeEvent() {
window.removeEventListener('resize', this.$_resizeHandler) if (this.resizeObserver) {
this.resizeObserver.disconnect()
this.resizeObserver = null
}
}, },
start() { start() {
import('@/plugin/webgl/fluid').then(_ => _.default(this.$el.querySelector('#canvas-fluid'))) import('@/plugin/webgl/fluid').then(_ => _.default(this.$el.querySelector('#canvas-fluid')))

Loading…
Cancel
Save