我想使用这个下降的五彩纸屑脚本作为两个独立div的背景,但无法弄清楚如何这样做。我尝试用重命名的变量创建第二个函数,但我不知所措。
http://codepen.io/anon/pen/obKBwj
NUM_CONFETTI = 350
COLORS = [[85,71,106], [174,61,99], [219,56,83], [244,92,68], [248,182,70]]
PI_2 = 2*Math.PI
canvas = document.getElementById "world"
context = canvas.getContext "2d"
window.w = 0
window.h = 0
resizeWindow = ->
window.w = canvas.width = window.innerWidth
window.h = canvas.height = window.innerHeight
window.addEventListener 'resize', resizeWindow, false
window.onload = -> setTimeout resizeWindow, 0
range = (a,b) -> (b-a)*Math.random() + a
drawCircle = (x,y,r,style) ->
context.beginPath()
context.arc(x,y,r,0,PI_2,false)
context.fillStyle = style
context.fill()
xpos = 0.5
document.onmousemove = (e) ->
xpos = e.pageX/w
window.requestAnimationFrame = do ->
window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
(callback) -> window.setTimeout(callback, 1000 / 60)
class Confetti
constructor: ->
@style = COLORS[~~range(0,5)]
@rgb = "rgba(#{@style[0]},#{@style[1]},#{@style[2]}"
@r = ~~range(2,6)
@r2 = 2*@r
@replace()
replace: ->
@opacity = 0
@dop = 0.03*range(1,4)
@x = range(-@r2,w-@r2)
@y = range(-20,h-@r2)
@xmax = w-@r
@ymax = h-@r
@vx = range(0,2)+8*xpos-5
@vy = 0.7*@r+range(-1,1)
draw: ->
@x += @vx
@y += @vy
@opacity += @dop
if @opacity > 1
@opacity = 1
@dop *= -1
@replace() if @opacity < 0 or @y > @ymax
if !(0 < @x < @xmax)
@x = (@x + @xmax) % @xmax
drawCircle(~~@x,~~@y,@r,"#{@rgb},#{@opacity})")
confetti = (new Confetti for i in [1..NUM_CONFETTI])
window.step = ->
requestAnimationFrame(step)
context.clearRect(0,0,w,h)
c.draw() for c in confetti
step()
答案 0 :(得分:0)
您的html中有两个canvas id="world"
,但ID应该是唯一的。
您不应该复制该功能,只能复制对象。像pen这样的东西可以给你一个良好的开端。
NUM_CONFETTI = 350
COLORS = [[85,71,106], [174,61,99], [219,56,83], [244,92,68], [248,182,70]]
PI_2 = 2*Math.PI
canvas = document.getElementById "world"
context = canvas.getContext "2d"
canvas2 = document.getElementById "world2"
context2 = canvas2.getContext "2d"
divs=[{canvas:canvas,context:context,w:0,h:0},{canvas:canvas2,context:context2,w:0,h:0}]
window.w = 0
window.h = 0
resizeWindow = ->
window.w = canvas.width = window.innerWidth
window.h = canvas.height = window.innerHeight
window.addEventListener 'resize', resizeWindow, false
window.onload = -> setTimeout resizeWindow, 0
range = (a,b) -> (b-a)*Math.random() + a
drawCircle = (x,y,r,style) ->
for div in divs
div.context.beginPath()
div.context.arc(x,y,r,0,PI_2,false)
div.context.fillStyle = style
div.context.fill()
xpos = 0.5
document.onmousemove = (e) ->
xpos = e.pageX/w
window.requestAnimationFrame = do ->
window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
(callback) -> window.setTimeout(callback, 1000 / 60)
class Confetti
constructor: ->
@style = COLORS[~~range(0,5)]
@rgb = "rgba(#{@style[0]},#{@style[1]},#{@style[2]}"
@r = ~~range(2,6)
@r2 = 2*@r
@replace()
replace: ->
@opacity = 0
@dop = 0.03*range(1,4)
@x = range(-@r2,w-@r2)
@y = range(-20,h-@r2)
@xmax = w-@r
@ymax = h-@r
@vx = range(0,2)+8*xpos-5
@vy = 0.7*@r+range(-1,1)
draw: ->
@x += @vx
@y += @vy
@opacity += @dop
if @opacity > 1
@opacity = 1
@dop *= -1
@replace() if @opacity < 0 or @y > @ymax
if !(0 < @x < @xmax)
@x = (@x + @xmax) % @xmax
drawCircle(~~@x,~~@y,@r,"#{@rgb},#{@opacity})")
for div in divs
div.confetti = (new Confetti for i in [1..NUM_CONFETTI])
window.step = ->
requestAnimationFrame(step)
for div in divs
div.context.clearRect(0,0,w,h)
c.draw() for c in div.confetti
step()
根据您的布局,您必须调整处理w
和h
的功能