I am trying to imitate 3d-model rotation with a sprite sheet. I found a perfect example on Codepen, but it was not responsive.
What I tried to do is to write divs, containers and spritesize (in script) in vw
, and then it is being checked in the window.resize
event. It does work, but unfortunately not DURING window resize.
I put my snippet and three pictures in the post —
- I opened website and everything is perfect – image
- I started to change the size of my browser window and as you can see something is wrong – image
- Now I tried to “rotate” the “model” with resized window and all is fine again – image
JavaScript
x
47
47
1
var spriteslider = document.createElement('div');
2
var clientWidth = document.getElementById('spritetarget').clientWidth;
3
4
document.body.appendChild(spriteslider);
5
6
spriteslider.slider = document.getElementById('spriteslider');
7
spriteslider.sprite = document.getElementById('spritetarget');
8
spriteslider.spritesize = clientWidth;
9
spriteslider.spritecount = 20;
10
spriteslider.pixelsperincrement = 5;
11
spriteslider.multiplier = spriteslider.lastmultiplier = 0;
12
13
Draggable.create(spriteslider, {
14
type: 'x',
15
trigger: spriteslider.slider,
16
bounds: {
17
minX: 0,
18
maxX: 0,
19
minY: 0,
20
maxY: 0
21
},
22
edgeResistance: 0,
23
cursor: 'e-resize',
24
onDrag: function() {
25
if (this.isDragging) {
26
var t = this.target;
27
t.multiplier = Math.floor(this.x / t.pixelsperincrement) + t.lastmultiplier;
28
// TweenLite.set(t.sprite, { backgroundPosition: (-t.multiplier * t.spritesize) + "px 0"});
29
TweenLite.set(t.sprite, {
30
backgroundPosition: (-t.multiplier * t.spritesize) + "px 0"
31
});
32
33
}
34
},
35
onDragEnd: function() {
36
var t = this.target;
37
t.lastmultiplier = t.multiplier % t.spritecount;
38
}
39
});
40
41
window.addEventListener('resize', function(event) {
42
var clientWidth = document.getElementById('spritetarget').clientWidth;
43
spriteslider.spritesize = clientWidth;
44
TweenLite.set(t.sprite, {
45
backgroundPosition: (-t.multiplier * t.spritesize) + "px 0"
46
});
47
}, true);
JavaScript
1
24
24
1
body {
2
text-align: center;
3
font: normal 12px sans-serif;
4
background: #000000;
5
color: #91E600;
6
}
7
8
.spriteslider {
9
margin: 20px auto;
10
padding: 60px;
11
width: 20vw;
12
height: 20vw;
13
background: #FCFEFC;
14
border-radius: 5px;
15
}
16
17
#spritetarget {
18
width: 20vw;
19
height: 20vw;
20
background-size: cover;
21
background-image: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/29123/heart.png);
22
/* horizontal spritesheet - image from http://preloaders.net */
23
background-repeat: repeat-x;
24
}
JavaScript
1
7
1
<div class='spriteslider' id='spriteslider'>
2
<div id='spritetarget'></div>
3
</div>
4
5
<p>Drag the box left/right to control the sprite's position.</p>
6
<script src='https://cdnjs.cloudflare.com/ajax/libs/gsap/latest/TweenMax.min.js'></script>
7
<script src='https://cdnjs.cloudflare.com/ajax/libs/gsap/latest/utils/Draggable.min.js'></script>
Advertisement
Answer
The issue is because you’re referencing t
in the window.resize
event handler, yet that variable has been defined in a different scope, and is not accessible from that location.
To fix this issue you can replace t
in that function with the spriteslider
variable, as that’s what t
is expected to contain. Try this:
JavaScript
1
46
46
1
var spriteslider = document.createElement('div');
2
var clientWidth = document.getElementById('spritetarget').clientWidth;
3
4
document.body.appendChild(spriteslider);
5
6
spriteslider.slider = document.getElementById('spriteslider');
7
spriteslider.sprite = document.getElementById('spritetarget');
8
spriteslider.spritesize = clientWidth;
9
spriteslider.spritecount = 20;
10
spriteslider.pixelsperincrement = 5;
11
spriteslider.multiplier = spriteslider.lastmultiplier = 0;
12
13
Draggable.create(spriteslider, {
14
type: 'x',
15
trigger: spriteslider.slider,
16
bounds: {
17
minX: 0,
18
maxX: 0,
19
minY: 0,
20
maxY: 0
21
},
22
edgeResistance: 0,
23
cursor: 'e-resize',
24
onDrag: function() {
25
if (this.isDragging) {
26
var t = this.target;
27
t.multiplier = Math.floor(this.x / t.pixelsperincrement) + t.lastmultiplier;
28
TweenLite.set(t.sprite, {
29
backgroundPosition: (-t.multiplier * t.spritesize) + "px 0"
30
});
31
32
}
33
},
34
onDragEnd: function() {
35
var t = this.target;
36
t.lastmultiplier = t.multiplier % t.spritecount;
37
}
38
});
39
40
window.addEventListener('resize', function(event) {
41
var clientWidth = document.getElementById('spritetarget').clientWidth;
42
spriteslider.spritesize = clientWidth;
43
TweenLite.set(spriteslider.sprite, {
44
backgroundPosition: (-spriteslider.multiplier * spriteslider.spritesize) + "px 0"
45
});
46
}, true);
JavaScript
1
24
24
1
body {
2
text-align: center;
3
font: normal 12px sans-serif;
4
background: #000000;
5
color: #91E600;
6
}
7
8
.spriteslider {
9
margin: 20px auto;
10
padding: 60px;
11
width: 20vw;
12
height: 20vw;
13
background: #FCFEFC;
14
border-radius: 5px;
15
}
16
17
#spritetarget {
18
width: 20vw;
19
height: 20vw;
20
background-size: cover;
21
background-image: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/29123/heart.png);
22
/* horizontal spritesheet - image from http://preloaders.net */
23
background-repeat: repeat-x;
24
}
JavaScript
1
7
1
<div class='spriteslider' id='spriteslider'>
2
<div id='spritetarget'></div>
3
</div>
4
5
<p>Drag the box left/right to control the sprite's position.</p>
6
<script src='https://cdnjs.cloudflare.com/ajax/libs/gsap/latest/TweenMax.min.js'></script>
7
<script src='https://cdnjs.cloudflare.com/ajax/libs/gsap/latest/utils/Draggable.min.js'></script>