2013-10-09 50 views
0

particlesDepthBlur不工作?d3 javascript - 爲什麼我的隨機數字不工作?

<canvas id="canvas"></canvas> 
<script src="http://d3js.org/d3.v3.min.js"></script> 
<script> 
var num = 20000; 

var canvas = document.getElementById("canvas"); 
var width = canvas.width = 960; 
var height = canvas.height = 500; 
var ctx = canvas.getContext("2d"); 

var particles = d3.range(num).map(function(i) { 
    return [Math.round(width*Math.random()), Math.round(height*Math.random())]; 
}); 

var particlesDepthBlur = (function() { Math.random();})(); 

d3.timer(step); 

function step() { 
    ctx.fillStyle = "rgba(0,0,0,1)"; 
    ctx.fillRect(0,0,width,height); 
    ctx.fillStyle = "rgba(255,255,255,particlesDepthBlur)"; 
    particles.forEach(function(p) { 
    p[0] += Math.round(2*Math.random()-1); 
    p[1] += Math.round(2*Math.random()-1) + 2; 
    if (p[0] < 0) p[0] = width; 
    if (p[0] > width) p[0] = 0; 
    if (p[1] < 0) p[1] = height; 
    if (p[1] > height) p[1] = 0; 
    drawPoint(p); 
    }); 
}; 

function drawPoint(p) { 
    ctx.fillRect(p[0],p[1],1,1); 
}; 
</script> 
<style> 
html, body { margin: 0; padding: 0; } 
</style> 

回答

1
var particlesDepthBlur = function() { 
    return Math.random(); 
}; 

ctx.fillStyle = "rgba(255,255,255," + particlesDepthBlur() + ")"; 
+0

燁,OP運行'的Math.random()'和與輸出無所事事,則函數返回'undefined'。 – Plato

+0

我其實是自己想出來的,對我來說很愚蠢。無論如何,我還有另一個問題......比這更復雜 – gogogadgetinternet

相關問題