-
Notifications
You must be signed in to change notification settings - Fork 0
/
skeleton-focal.js
250 lines (228 loc) · 5.5 KB
/
skeleton-focal.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/* eslint-disable max-len */
/* eslint-disable-next-line max-len */
import {html, PolymerElement} from '@polymer/polymer/polymer-element.js';
import {GestureEventListeners} from '@polymer/polymer/lib/mixins/gesture-event-listeners.js';
import '@polymer/iron-image/iron-image.js';
/**
* `skeleton-focal`
*
*
* @customElement
* @polymer
* @demo demo/index.html
*/
class SkeletonFocal extends GestureEventListeners(PolymerElement) {
/**
* @return {!HTMLTemplateElement}
*/
static get template() {
return html`
<style is="custom-style">
:host {
display: block;
position: relative;
box-sizing: border-box;
-webkit-user-select: none;
-webkit-user-drag: none;
background-color: black;
margin: 2rem auto;
width: 100%;
max-width: 100%;
height: auto;
/* Default icon size */
--focal-icon-size: 2rem;
/* Styles for the icon */
--focal-icon: {
};
}
#drag {
display: block;
position: absolute;
height: 0;
width: 0;
top: 50%;
left: 50%;
z-index: 3;
opacity: 1;
box-sizing: border-box;
transform: translate(-50%, -50%);
}
#dragIcon {
display: block;
height: var(--focal-icon-size);
width: var(--focal-icon-size);
margin-top: calc(-1 * var(--focal-icon-size) / 2);
margin-left: calc(-1 * var(--focal-icon-size) / 2);
box-sizing: border-box;
cursor: move;
box-shadow: 0 0 3px rgba(0, 0, 0, .8), 0 0 4px rgba(0, 0, 0, .4);
border-radius: 50%;
background-color: rgba(0, 0, 0, .3);
border: 5px solid white;
transition: opacity ease-in-out 1s;
@apply --focal-icon;
}
iron-image {
display: block;
position: relative;
z-index: 1;
margin: 0;
width: inherit;
height: inherit;
max-width: 100%;
--iron-image-width: 100%;
-webkit-user-select: none;
-webkit-user-drag: none;
}
[faded] {
opacity: 0;
}
</style>
<iron-image src="[[src]]" placeholder="[[placeholder]]" on-loaded-changed="_loaded" prevent-load\$="[[preventLoad]]" crossorigin\$="[[crossorigin]]" preload="" fade=""></iron-image>
<div id="drag" faded\$="[[!loaded]]" style\$="left: [[_calcPercentage(x)]]; top: [[_calcPercentage(y)]];" on-track="_move">
<div id="dragIcon" on-dblclick="reset"></div>
</div>
`;
}
/**
* @return {string}
*/
static get is() {
return 'skeleton-focal';
}
/**
* @return {object}
*/
static get properties() {
return {
/**
* The URL of an image.
*/
src: {
type: String,
value: '',
},
/**
* This image will be used as a background/placeholder until the src
* image has loaded. Use of a data-URI for placeholder is encouraged
* for instant rendering.
*/
placeholder: {
type: String,
value: null,
},
/**
* When true, the image is prevented from loading and any placeholder is
* shown. This may be useful when a binding to the src property is known
* to be invalid, to prevent 404 requests.
*/
preventLoad: {
type: Boolean,
value: false,
},
/**
* CORS enabled images support: https://developer.mozilla.org/en-US/docs/Web/HTML/CORS_enabled_image
*/
crossorigin: {
type: String,
value: null,
},
/**
* X Axis position (0 - 1)
*/
x: {
type: Number,
value: 0.5,
reflectToAttribute: true,
notify: true,
},
/**
* Y Axis position (0 - 1)
*/
y: {
type: Number,
value: 0.5,
reflectToAttribute: true,
notify: true,
},
/**
* Read-only value that is true when the image is loaded.
*/
loaded: {
type: Boolean,
value: false,
notify: true,
readOnly: true,
},
};
}
/**
* Constructor
*/
constructor() {
super();
}
/**
* Listen for loaded event and recalculate position
*
* @param {object} e
* @private
*/
_loaded(e) {
const status = !!e.detail.value;
this._setLoaded(status);
}
/**
* Returns the percentage that will to position the icon
*
* @param {number} value
* @return {string}
* @private
*/
_calcPercentage(value) {
return `${value * 100}%`;
}
/**
* Recalculate the coordinates after is dragged
*
* @param {object} event
* @private
*/
_move(event) {
const width = this.offsetWidth;
const height = this.offsetHeight;
const y = event.detail.ddy ? event.detail.ddy : 0;
const x = event.detail.ddx ? event.detail.ddx : 0;
let finalY = y / height;
let finalX = x / width;
let finalTop = this.y + finalY;
let finalLeft = this.x + finalX;
finalTop = this._normalize(finalTop);
finalLeft = this._normalize(finalLeft);
this.y = Number(finalTop.toFixed(3));
this.x = Number(finalLeft.toFixed(3));
}
/**
* Returns a valid value
*
* @param {number} value
* @return {*}
* @private
*/
_normalize(value) {
if (value < 0) {
value = 0;
} else if (value > 1) {
value = 1;
}
return value;
}
/**
* Reset position
*/
reset() {
this.y = 0.5;
this.x = 0.5;
}
}
window.customElements.define(SkeletonFocal.is, SkeletonFocal);