2012-12-06 45 views
2

有人帮我画光射线像这样:绘制在画布光线

Example image

我试过很多次,但不能。不需要完美地匹配颜色,至少基本线条和渐变。

+0

我只看到你的照片了一堆非英语张贴... – luiges90

+0

或者我可以改变图像颜色? – Arti

回答

5

http://jsbin.com/uhuwud/1/edit

(function(){ 
    var can = document.getElementById("lightray"), 
     ctx = can.getContext('2d'), 
     wid = can.width, 
     hei = can.height, 
     ang = -90 * Math.PI/180, 
     spn = 80 * Math.PI/180, 
     ctr = {x: 200, y:200}, 
     rad = 200, 
     grd = ctx.createRadialGradient(ctr.x, ctr.y, 0, ctr.x, ctr.y, rad); 

    grd.addColorStop(0, 'rgba(0,200,255,.25)'); 
    grd.addColorStop(0.75, 'rgba(0,0,0,0.25)'); 
    grd.addColorStop(1, 'rgba(0,0,0,0)'); 
    ctx.fillStyle = grd; 



    (function draw(){ 
    ctx.clearRect(0,0,wid,hei); 
    ang = Math.sin(new Date()/1000) * Math.PI/2; 
    spn = (Math.abs(Math.sin(new Date()/1000/3)) * 120 + 30) * Math.PI/180; 

// The below code is the pertinent part 
// the concept is that I'm drawing a radial gradient in an arc, and decreasing the span 
// of the arc some number of times. This will create the side-to-side fading. 
    for (var i = 2; i < 10; i+=0.1){ 
     ctx.beginPath(); 
     ctx.moveTo(ctr.x,ctr.y); 
     ctx.arc(ctr.x, ctr.y, rad, ang - spn/i, ang + spn/i, false); 
     ctx.closePath(); 
     ctx.fill(); 
    } 
// the above code 

    webkitRequestAnimationFrame(draw); 
    })(); 
})(); 
+0

真棒!!!!!!! – Arti

+0

根据你的代码我创建了下面的小提琴:http://jsfiddle.net/yqd0pg48/但我不明白为什么这些光线不混合 - 混合红色和绿色应导致黄色交集。我尝试过各种复合模式,但没有运气。任何线索? – jesper

+0

@jesper如果你想交点为黄色,你可以使用'ctx.globalCompositeOperation ='lighter';' – Shmiddty