1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
| (function() {
// https://codepen.io/jlong64/pen/kXRxpA
// This expects to exist in an environment which has a background that's black. and has a canvas element named 'canvas'
// https://coffeescript-cookbook.github.io/chapters/math/generating-random-numbers
// The main canvas and context.
var boltAlphaDivisor, boltAlphaMin, boltCount, boltFadeDuration, boltFlashDuration, bolts, canvas, context, direction, flashOpacity, fps, height, lastFrame, lastLaunch, launchBolt, maxBoltInterval, maxBolts, perSec, recursiveLaunchBolt, scale, setCanvasSize, startx, starty, tick, totalBoltDuration, width;
canvas = document.getElementById("canvas");
context = canvas.getContext("2d");
boltCount = 1;
direction = 0.00;
lastLaunch = 0;
perSec = 1000.0;
maxBolts = 15;
boltAlphaMin = 0.75;
boltAlphaDivisor = 50;
maxBoltInterval = 1;
// The size and scale of the canvas. The size will get set via setCanvasSize to match the size of the window. Change the scale to get a more or less pixel-y effect.
width = 0.0;
height = 0.0;
scale = .50;
//Set the starting point of bolts to the center of the frame
startx = window.innerWidth;
starty = window.innerHeight;
// The frames per second of the simulation. We also keep track of the time the previous frame occurred so that we know the duration between each frame.
fps = 30.0;
lastFrame = (new Date()).getTime();
// The screen will flash briefly when the flash opacity is set. The opacity will dissipate quickly over time.
flashOpacity = 0.0;
//# Disabled for our use case
// When a bolt appears, it will be drawn at full opacity for the flash duration, and then will fade out gradually over the fade duration.
boltFlashDuration = 0; //# was 0.5
boltFadeDuration = 60; //# extend the duration a bit
totalBoltDuration = boltFlashDuration + boltFadeDuration;
// The list of bolts. A bolt will get added to this list on creation, and then will be automatically removed after it's done fading out.
bolts = [];
// Sets the size of the canvas and each bolt canvas.
setCanvasSize = function() {
var bolt, j, len;
canvas.setAttribute("width", window.innerWidth);
canvas.setAttribute("height", window.innerHeight);
for (j = 0, len = bolts.length; j < len; j++) {
bolt = bolts[j];
bolt.canvas.width = window.innerWidth;
bolt.canvas.height = window.innerHeight;
}
width = Math.ceil(window.innerWidth / scale);
return height = Math.ceil(window.innerHeight / scale);
};
// Launch a bolt!!!
launchBolt = function(x, y, length, direction) {
var boltCanvas, boltContext, boltDurationOffset;
//flashOpacity = 0.15 + Math.random() * 0.2 # Set the flash opacity.
flashOpacity = 0;
//Create the bolt canvas.
boltCanvas = document.createElement("canvas");
boltCanvas.width = window.innerWidth;
boltCanvas.height = window.innerHeight;
boltContext = boltCanvas.getContext("2d");
boltContext.scale(scale, scale);
boltDurationOffset = Math.floor(Math.random() * totalBoltDuration);
//console.log("launchBolt[" + boltCount + "]: Direction: " + direction + " Length: " + length + " BoltDurationOffset: "+ boltDurationOffset + "/"+ totalBoltDuration)
// Add the bolt to the list.
//#
bolts.push({
canvas: boltCanvas,
duration: boltDurationOffset
});
// Launch it!!
return recursiveLaunchBolt(x, y, length, direction, boltContext);
};
// Recursive bolt action.
recursiveLaunchBolt = function(x, y, length, direction, boltContext) {
var boltInterval, originalDirection;
originalDirection = direction;
// We draw the bolt incrementally to get a nice animated effect.
return boltInterval = setInterval((function() {
var alpha, i, x1, y1;
if (length <= 0) {
clearInterval(boltInterval);
return;
}
i = 0;
while (i++ < Math.floor(1) && length > 0) {
x1 = Math.floor(x);
y1 = Math.floor(y);
x += Math.cos(direction);
y -= Math.sin(direction);
length--;
if (x1 !== Math.floor(x) || y1 !== Math.floor(y)) {
alpha = Math.min(boltAlphaMin, length / boltAlphaDivisor);
boltContext.fillStyle = `rgba(255, 195, 235, ${alpha})`;
boltContext.fillRect(x1, y1, 2.5, 2.5);
direction = originalDirection + (-Math.PI / 8.0 + Math.random() * (Math.PI / 4.0));
if (Math.random() > 0.991) {
recursiveLaunchBolt(x1, y1, length * (0.3 + Math.random() * 0.4), originalDirection + (-Math.PI / 6.0 + Math.random() * (Math.PI / 3.0)), boltContext);
} else if (Math.random() > 0.95) {
recursiveLaunchBolt(x1, y1, length, originalDirection + (-Math.PI / 6.0 + Math.random() * (Math.PI / 3.0)), boltContext);
length = 0;
}
}
}
return void 0;
}), 10);
};
// This will get fired at a constant framerate.
tick = function() {
var _d, _delta, _lastLaunch, bolt, elapsed, frame, j, len, length, x, y;
// Keep track of the frame time.
frame = (new Date()).getTime();
elapsed = (frame - lastFrame) / perSec;
lastFrame = frame;
// Clear the canvas.
context.clearRect(0.0, 0.0, window.innerWidth, window.innerHeight);
// Fire a bolt every once in a while.
if (Math.random() > 0.99) {
x = startx; //Math.floor -10.0 + Math.random() * (width + 20.0)
y = starty; //Math.floor 5.0 + Math.random() * (height / 3.0)
length = Math.floor(height / 10.0 + Math.random() * (height / 2.0));
_d = Math.random() * 361;
_delta = (frame - lastLaunch) / perSec;
_lastLaunch = lastLaunch;
lastLaunch = frame;
// console.log("["+_delta+"]Im gunna launch a bolt X: " + x + " Y: " + y + " Direction: " + _d + " Length: " + length )
if (boltCount < maxBolts) {
launchBolt(x, y, length, _d);
} else {
// console.log("Bolt Count ("+boltCount+") Exceeds maxBolts: "+maxBolts+".")
if (_delta > maxBoltInterval) {
//too much time has elapsed between bolts. We should increment the maxBolt
//console.log("HOWEVER it's been too long since we last launched a bolt. and maxBoltInterval is "+ maxBoltInterval )
maxBolts = maxBolts + 1;
//console.log("maxBolts now "+ maxBolts )
launchBolt(x, y, length, _d);
}
}
}
// Draw each bolt.
for (boltCount = j = 0, len = bolts.length; j < len; boltCount = ++j) {
bolt = bolts[boltCount];
bolt.duration += elapsed;
//console.log("Bolt[" + i + "] Duration: "+ bolt.duration)
if (bolt.duration >= totalBoltDuration) {
//console.log("Bolt[" + boltCount + "] SPLICE "+ bolt.duration + " " + totalBoltDuration)
// Bolt culling.
bolts.splice(boltCount, 1);
boltCount--;
return;
}
context.globalAlpha = Math.max(0.0, Math.min(1.0, (totalBoltDuration - bolt.duration) / boltFadeDuration));
context.drawImage(bolt.canvas, 0.0, 0.0);
}
return void 0;
};
// Start it up!!!
window.addEventListener("resize", setCanvasSize);
setCanvasSize();
//console.log("width " + width);
//console.log("height " + height);
//console.log("startx " + startx);
//console.log("starty " + starty);
setInterval(tick, (perSec / 2) / fps);
}).call(this);
|