-
Notifications
You must be signed in to change notification settings - Fork 0
/
cork-360-image-viewer.js
146 lines (119 loc) · 3.73 KB
/
cork-360-image-viewer.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
import {PolymerElement, html} from "@polymer/polymer"
import template from "./cork-360-image-viewer.html"
export class Cork360ImageViewer extends PolymerElement {
static get template() {
return html([template]);
}
static get properties() {
return {
frame : {
type : Number,
value : 0,
observer : 'render'
},
totalFrames : {
type : Number,
value : 1
},
// mouse pixels that must be moved per frame
dragRatio : {
type : Number,
value : 100
},
spinFps : {
type : Number,
value : 1
}
}
}
constructor() {
super();
this._onWindowMouseMove = this._onWindowMouseMove.bind(this);
this._onWindowMouseUp = this._onWindowMouseUp.bind(this);
this._onMouseDown = this._onMouseDown.bind(this);
this._onScroll = this._onScroll.bind(this);
}
connectedCallback() {
super.connectedCallback();
this._attachEventHandlers();
}
disconnectedCallback() {
super.disconnectedCallback();
this._detachEventHandlers();
}
_attachEventHandlers() {
window.addEventListener('mousemove', this._onWindowMouseMove);
window.addEventListener('mouseup', this._onWindowMouseUp);
this.addEventListener('mousedown', this._onMouseDown);
this.addEventListener('mousewheel', this._onScroll);
this.$.img.addEventListener('DOMMouseScroll', this._onScroll);
}
_detachEventHandlers() {
window.removeEventListener('mousemove', this._onWindowMouseMove);
window.removeEventListener('mouseup', this._onWindowMouseUp);
this.removeEventListener('mousedown', this._onMouseDown);
this.removeEventListener('mousewheel', this._onScroll);
this.$.img.removeEventListener('DOMMouseScroll', this._onScroll);
}
_onWindowMouseUp() {
this.mousedown = false;
}
_onMouseDown(e) {
this.mousedown = true;
this.startMouseEvent = e;
this.startFrame = this.frame;
this.stopSpin();
}
_onWindowMouseMove(e) {
if( !this.mousedown ) return;
let delta = e.clientX - this.startMouseEvent.clientX;
let pxPerFrame = this.dragRatio / this.totalFrames;
let numFramesMoved = Math.round(delta / pxPerFrame);
this.frame = this.startFrame + numFramesMoved;
}
_onScroll(e) {
if( this.mousedown ) return;
this.stopSpin();
let delta = e.deltaY !== undefined ? e.deltaY/10 : e.detail * 12;
let pxPerFrame = this.dragRatio / this.totalFrames;
let numFramesMoved = Math.round(delta / pxPerFrame);
this.frame = this.frame + numFramesMoved;
}
spin() {
this.spinTimer = setInterval(() => this.frame++, (1 / this.spinFps) * 1000);
this.dispatchEvent(new CustomEvent('spin-start'));
}
stopSpin() {
if( !this.spinTimer ) return;
clearInterval(this.spinTimer);
this.spinTimer = null;
this.dispatchEvent(new CustomEvent('spin-stop'));
}
preload() {
return new Promise((resolve, reject) => {
let done = 0;
function loaded() {
done++;
if( done !== this.totalFrames ) return;
resolve();
};
for( let i = 0; i < this.totalFrames; i++ ) {
let img = new Image();
img.src = this.frameToImgPath(i);
img.onload = loaded.bind(this);
img.onerror = loaded.bind(this);
}
});
}
render() {
let currentFrame = this.frame % this.totalFrames;
if( currentFrame < 0 ) currentFrame = this.totalFrames + currentFrame;
if( !this.frameToImgPath(currentFrame) ) return;
this.$.img.src = this.frameToImgPath(currentFrame);
this.dispatchEvent(new CustomEvent('frame-render', {detail: {frame: currentFrame}}));
}
frameToImgPath(frame) {
// Implement Me!
}
}
window.customElements.define('cork-360-image-viewer', Cork360ImageViewer);