Javascript 多次坠落的五彩纸屑潜水艇
我想用这个掉落的五彩纸屑脚本作为两个单独的div的背景,但我不知道怎么做。我试图用重命名的变量创建第二个函数,但我不知所措Javascript 多次坠落的五彩纸屑潜水艇,javascript,coffeescript,Javascript,Coffeescript,我想用这个掉落的五彩纸屑脚本作为两个单独的div的背景,但我不知道怎么做。我试图用重命名的变量创建第二个函数,但我不知所措 NUM_五彩纸屑=350 颜色=[[85,71106]、[174,61,99]、[219,56,83]、[244,92,68]、[248182,70]] PI_2=2*Math.PI canvas=document.getElementById“world” context=canvas.getContext“2d” window.w=0 window.h=0 resiz
NUM_五彩纸屑=350
颜色=[[85,71106]、[174,61,99]、[219,56,83]、[244,92,68]、[248182,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
画圈=(x、y、r、样式)->
context.beginPath()
弧(x,y,r,0,PI_2,false)
context.fillStyle=样式
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||
(回调)->window.setTimeout(回调,1000/60)
班级五彩纸屑
构造函数:->
@样式=颜色[~~范围(0,5)]
@rgb=“rgba(#{@style[0]},#{@style[1]},#{@style[2]}”
@r=~~范围(2,6)
@r2=2*@r
@替换()
替换:->
@不透明度=0
@dop=0.03*范围(1,4)
@x=范围(-@r2,w-@r2)
@y=范围(-20,h-@r2)
@xmax=w-@r
@ymax=h-@r
@vx=范围(0,2)+8*xpos-5
@vy=0.7*@r+范围(-1,1)
抽签:->
@x+=@vx
@y+=@vy
@不透明度+=@dop
如果@opacity>1
@不透明度=1
@dop*=-1
@如果@opacity<0或@y>@ymax,则替换()
如果!(0<@x<@xmax)
@x=(@x+@xmax)%@xmax
画圈(~~@x,~~@y,@r,“{@rgb},{@opacity})”)
五彩纸屑=(为[1..NUM_五彩纸屑]中的i添加新五彩纸屑)
window.step=->
requestAnimationFrame(步骤)
context.clearRect(0,0,w,h)
c、 在五彩纸屑中为c绘制()
步骤(
在html中有两个canvas id=“world”
,但id应该是唯一的
不应复制函数,而应复制对象。像这样的事情应该会给你一个好的开始
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()
NUM_五彩纸屑=350
颜色=[[85,71106]、[174,61,99]、[219,56,83]、[244,92,68]、[248182,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
画圈=(x、y、r、样式)->
对于div中的div
div.context.beginPath()
弧(x,y,r,0,PI_2,false)
div.context.fillStyle=样式
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||
(回调)->window.setTimeout(回调,1000/60)
班级五彩纸屑
构造函数:->
@样式=颜色[~~范围(0,5)]
@rgb=“rgba(#{@style[0]},#{@style[1]},#{@style[2]}”
@r=~~范围(2,6)
@r2=2*@r
@替换()
替换:->
@不透明度=0
@dop=0.03*范围(1,4)
@x=范围(-@r2,w-@r2)
@y=范围(-20,h-@r2)
@xmax=w-@r
@ymax=h-@r
@vx=范围(0,2)+8*xpos-5
@vy=0.7*@r+范围(-1,1)
抽签:->
@x+=@vx
@y+=@vy
@不透明度+=@dop
如果@opacity>1
@不透明度=1
@dop*=-1
@如果@opacity<0或@y>@ymax,则替换()
如果!(0<@x<@xmax)
@x=(@x+@xmax)%@xmax
画圈(~~@x,~~@y,@r,“{@rgb},{@opacity})”)
对于div中的div
div.confetti=(在[1..NUM_confetti]中为i添加了新的五彩纸屑)
window.step=->
requestAnimationFrame(步骤)
对于div中的div
div.context.clearRect(0,0,w,h)
c、 为五彩纸屑中的c绘制()
步骤(
根据您的布局,您必须调整处理w
和h
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()