-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
174 lines (149 loc) · 4.55 KB
/
script.js
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
//*** SHIM ***
window.requestAnimFrame = (function() {
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function(callback) {
window.setTimeout(callback, 1000 / 60);
};
})();
(function() {
"use strict";
//************
//VARIABLES
//************
var _Canvas;
let _frontImageSrc = 'http://insidedown.com/codepen/stock/mountain-tan.jpg';
let _backImageSrc = 'http://insidedown.com/codepen/stock/mountain.jpg';
let _frontImage;
let _backImage;
let _blackMask;
let _mouseX = 0;
let _mouseY = 0;
let _maskCount = 25;
let _tweenTime = 0.5;
let _pauseTime = 0.25;
let _delayTime = 0.08;
let _maskArray = [];
let _srcArray = ["http://insidedown.com/codepen/stock/newstain1.png", "http://insidedown.com/codepen/stock/newstain2.png", "http://insidedown.com/codepen/stock/newstain3.png"];
//************
//METHODS
//************
function init() {
_Canvas = new Canvas({stage:document.getElementById('stage')});
_backImage = new MaskedImage({src:_backImageSrc});
_frontImage = new MaskedImage({src:_frontImageSrc});
for(let i=0;i<_maskCount;i++){
let ranSrc = _srcArray[Math.floor(Math.random() * _srcArray.length)];
let mask = new MaskedImage({src:ranSrc, delay:i, width:300});
_maskArray.push(mask);
}
addListeners();
}
//************
//EVENTS
//************
function addListeners() {
_Canvas.el.addEventListener('mousemove', onCanvasMouseMove);
_Canvas.el.addEventListener('mouseout', onCanvasMouseOut);
}
function onCanvasMouseMove(event) {
_mouseX = event.pageX - $(this).offset().left;
_mouseY = event.pageY - $(this).offset().top;
}
function onCanvasMouseOut(event) {
}
function onEnterFrame() {
_Canvas.clearStage();
drawStage();
window.requestAnimFrame(onEnterFrame, 60);
}
function drawStage() {
_Canvas.context.save();
for(let i=0;i<_maskCount;i++){
let mask = _maskArray[i];
mask.tweenDraw();
}
//_blackMask.draw(_mouseX,_mouseY);
_Canvas.context.globalCompositeOperation = 'source-in';
_backImage.draw();
_Canvas.context.globalCompositeOperation = 'destination-over';
_frontImage.draw();
_Canvas.context.restore();
}
//************
//CLASSES
//************
class MaskedImage {
constructor(options) {
this.hasImg = false;
this.img = new Image();
this.empty = {scale:0, alpha:1, x:0, y:0};
this.delay = options.delay;
this.rotation = Math.random() * 360;
this.width = options.width;
this.halfWidth = this.width/2;
this.img.src = options.src;
this.img.onload = function() {
this.hasImg = true;
if(this.delay){
setTimeout(function() {this.scale();}.bind(this), this.delay*(_delayTime * 1000));
}
this.draw();
}.bind(this);
}
draw(x=0,y=0) {
if(this.hasImg) {
_Canvas.context.drawImage(this.img,x,y);
}
}
tweenDraw() {
if(this.hasImg) {
let curWidth = this.width * this.empty.scale;
_Canvas.context.save();
_Canvas.context.globalAlpha = this.empty.alpha;
_Canvas.context.translate(this.empty.x, this.empty.y);
_Canvas.context.rotate(this.rotation * Math.PI / 180);
_Canvas.context.scale(1.5 * (curWidth/this.width), 1.5*(curWidth/this.width));
_Canvas.context.translate(-this.empty.x, -this.empty.y);
_Canvas.context.drawImage(this.img,this.empty.x-this.halfWidth,this.empty.y-this.halfWidth);
_Canvas.context.globalAlpha = 1;
_Canvas.context.restore();
}
}
scale() {
this.empty.x = _mouseX;
this.empty.y = _mouseY;
this.rotation = Math.random() * 360;
TweenMax.fromTo(this.empty, _tweenTime, {alpha:1, scale:0},{alpha:1, scale:1, onComplete:function(){
setTimeout(this.fadeOut.bind(this), _pauseTime * 1000);
}.bind(this)
});
}
fadeOut() {
TweenMax.to(this.empty, _tweenTime,{alpha:0, onComplete:this.scale.bind(this)});
}
}
class Canvas {
constructor(options) {
this._stage = options.stage;
this._stageWidth = this._stage.width = window.innerWidth;
this._stageHeight = this._stage.height = window.innerHeight;
this._stageContext = this._stage.getContext('2d');
}
// clear stage of current content
clearStage(options) {
if(typeof options === "undefined") {
this._stageContext.clearRect(0,0,this._stageWidth, this._stageHeight);
}
}
get width() { return this._stageWidth; }
get height() { return this._stageHeight; }
get el() {return this._stage; }
get context() {return this._stageContext; }
} //end Canvas class
init();
onEnterFrame();
})();