- javascript variable scope/closure in loop after timeout
- function update()
- {
- var i;
- this.context.lineWidth = BRUSH_SIZE;
- this.context.strokeStyle = "rgba(" + COLOR[0] + ", " + COLOR[1] + ", " + COLOR[2] + ", " + BRUSH_PRESSURE + ")";
- for (i = 0; i < scope.painters.length; i++)
- {
- scope.context.beginPath();
- var dx = scope.painters[i].dx;
- var dy = scope.painters[i].dy;
- scope.context.moveTo(dx, dy);
- var dx1 = scope.painters[i].ax = (scope.painters[i].ax + (scope.painters[i].dx - scope.mouseX) * scope.painters[i].div) * scope.painters[i].ease;
- scope.painters[i].dx -= dx1;
- var dx2 = scope.painters[i].dx;
- var dy1 = scope.painters[i].ay = (scope.painters[i].ay + (scope.painters[i].dy - scope.mouseY) * scope.painters[i].div) * scope.painters[i].ease;
- scope.painters[i].dy -= dy1;
- var dy2 = scope.painters[i].dy;
- scope.context.lineTo(dx2, dy2);
- scope.context.stroke();
- for(j=FADESTEPS;j>0;j--)
- {
- setTimeout(function()
- {
- var x=dx,y=dy,x2=dx2,y2=dy2;
- scope.context.beginPath();
- scope.context.lineWidth=BRUSH_SIZE+1;
- scope.context.moveTo(x, y);
- scope.context.strokeStyle = "rgba(" + 255 + ", " + 255 + ", " + 255 + ", " + .3 + ")";
- scope.context.lineTo(x2, y2);
- scope.context.stroke();
- scope.context.lineWidth=BRUSH_SIZE;
- },
- DURATION/j);
- }
- }
- }
- for(j=FADESTEPS;j>0;j--) {
- (function(x,y,x2,y2) {
- setTimeout(function() {
- scope.context.beginPath();
- scope.context.lineWidth=BRUSH_SIZE+1;
- scope.context.moveTo(x, y);
- scope.context.strokeStyle = "rgba(" + 255 + ", " + 255 + ", " + 255 + ", " + .3 + ")";
- scope.context.lineTo(x2, y2);
- scope.context.stroke();
- scope.context.lineWidth=BRUSH_SIZE;
- },
- DURATION/j);
- })(dx, dy, dx2, dy2);
- }
- `<script>
- for(j=10;j>0;j--)
- {
- var fn = function(ind){return function()
- {
- console.log(ind);
- };
- }(j);
- setTimeout(fn,
- 1000);
- }
- </script>`
- for(j=FADESTEPS;j>0;j--)
- {
- setTimeout(function(x,y,x2,y2)
- {
- scope.context.beginPath();
- scope.context.lineWidth=BRUSH_SIZE+1;
- scope.context.moveTo(x, y);
- scope.context.strokeStyle = "rgba(" + 255 + ", " + 255 + ", " + 255 + ", " + .3 + ")";
- scope.context.lineTo(x2, y2);
- scope.context.stroke();
- scope.context.lineWidth=BRUSH_SIZE;
- },
- DURATION/j,dx,dy,dx2,dy2);
- }
- for (var i = 0; i < 10; i++) setTimeout(function() { console.log(i)}, 10 )
- 10
- 10
- // ...
- this.interval = setInterval(update, REFRESH_RATE);
- var _points = [];
- function update() {
- var i;
- this.context.lineWidth = BRUSH_SIZE;
- this.context.strokeStyle = "rgba(" + COLOR[0] + ", " + COLOR[1] + ", " + COLOR[2] + ", " + BRUSH_PRESSURE + ")";
- for (i = 0; i < scope.painters.length; i++) {
- scope.context.beginPath();
- var dx = scope.painters[i].dx;
- var dy = scope.painters[i].dy;
- scope.context.moveTo(dx, dy);
- var dx1 = scope.painters[i].ax = (scope.painters[i].ax + (scope.painters[i].dx - scope.mouseX) * scope.painters[i].div) * scope.painters[i].ease;
- scope.painters[i].dx -= dx1;
- var dx2 = scope.painters[i].dx;
- var dy1 = scope.painters[i].ay = (scope.painters[i].ay + (scope.painters[i].dy - scope.mouseY) * scope.painters[i].div) * scope.painters[i].ease;
- scope.painters[i].dy -= dy1;
- var dy2 = scope.painters[i].dy;
- scope.context.lineTo(dx2, dy2);
- scope.context.stroke();
- _points.push([dx, dy, dx2, dy2]);
- clear();
- }
- }
- function clear(){
- if(_points.length < FADESTEPS){
- return;
- }
- var p = _points.shift();
- if(!p){
- return;
- }
- var x = p[0],
- y = p[1],
- x2 = p[2],
- y2 = p[3];
- scope.context.beginPath();
- scope.context.lineWidth = BRUSH_SIZE + 1;
- scope.context.moveTo(x, y);
- scope.context.strokeStyle = "rgba(" + 255 + ", " + 255 + ", " + 255 + ", " + .3 + ")";
- scope.context.lineTo(x2, y2);
- scope.context.stroke();
- scope.context.lineWidth = BRUSH_SIZE;
- }