-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblox-avatar.js
140 lines (133 loc) · 4.38 KB
/
blox-avatar.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
import {html, PolymerElement} from '@polymer/polymer/polymer-element.js';
/**
* `blox-avatar`
* select, resize, rotate and save an avatar image
*
* @customElement
* @polymer
* @demo demo/index.html
*/
class BloxAvatar extends PolymerElement {
static get template() {
return html`
<style>
:host {
display: block;
}
.file {
display: none;
}
</style>
<div class="rotate-icon" on-click="_rotate">Rotate</div>
<input type="file" name="file" id="file" class="file" on-change="_fileSelected" accept=".jpg, .jpeg, .png, .gif" />
<canvas
id="canvas"
class="canvas"
width="{{canvasWidth}}"
height="{{canvasHeight}}"
on-mousedown="_panStart"
on-mouseup="_panEnd"
on-mousemove="_panMove"
on-touchstart="_panStart"
on-touchend="_panEnd"
on-touchmove="_panMove">
Your browser does not support HTML5 Canvas.
</canvas>
`;
}
static get properties() {
return {
canvasWidth: {
type: Number,
value: 250,
},
canvasHeight: {
type: Number,
value: 250,
},
};
}
_panStart(event) {
event.preventDefault();
this.mouseDown = true;
if (event.clientX===undefined) {
this.startX = event.touches[0].clientX - this.endX;
this.startY = event.touches[0].clientY - this.endY;
} else {
this.startX = event.clientX - this.endX;
this.startY = event.clientY - this.endY;
}
}
_panEnd() {
this.mouseDown = false;
this._drawFile();
}
_panMove(event) {
if (this.mouseDown) {
if (event.clientX===undefined) {
this.endX = event.touches[0].clientX - this.startX;
this.endY = event.touches[0].clientY - this.startY;
} else {
this.endX = event.clientX - this.startX;
this.endY = event.clientY - this.startY;
}
}
}
_panStart(event) {
event.preventDefault();
this.mouseDown = true;
if (event.clientX===undefined) {
this.startX = event.touches[0].clientX - this.endX;
this.startY = event.touches[0].clientY - this.endY;
} else {
this.startX = event.clientX - this.endX;
this.startY = event.clientY - this.endY;
}
}
_drawFile(withArc=true, withShadow=true) {
if (this.image && this.canvasWidth && this.canvasHeight && this.scale && this.context) {
let myscale = (this.canvasHeight/this.image.height)*this.scale;
let px = 2*this.endX / (myscale);
let py = 2*this.endY / (myscale);
let rad = this.angle * Math.PI / 180;
let nx = px * Math.cos(-rad) - py * Math.sin(-rad);
let ny = px * Math.sin(-rad) + py * Math.cos(-rad);
this.context.globalAlpha = 1.0;
this.context.fillStyle = '#000';
this.context.fillRect(0, 0, this.canvasWidth, this.canvasHeight);
this.context.save();
this.context.translate(this.canvasWidth * 0.5, this.canvasHeight * 0.5);
this.context.scale(myscale, myscale);
this.context.rotate(this.angle * Math.PI / 180);
this.context.drawImage(this.image, - 0.5 * (this.image.width - nx),
- 0.5*(this.image.height - ny));
this.context.restore();
if (withShadow) {
this.context.globalAlpha = 0.5;
this.context.fillRect(0, 0, this.canvasWidth, this.canvasHeight);
this.context.save();
this.context.beginPath();
this.context.arc(this.canvasWidth / 2, this.canvasHeight / 2,
Math.min(100, 100) / 2, 0, 2 * Math.PI, false);
this.context.clip();
this.context.globalAlpha = 1;
this.context.translate(this.canvasWidth * 0.5, this.canvasHeight * 0.5);
this.context.scale(myscale, myscale);
this.context.rotate(this.angle * Math.PI / 180);
this.context.drawImage(this.image, - 0.5 * (this.image.width - nx),
- 0.5*(this.image.height - ny));
this.context.restore();
this.context.save();
}
if (withArc) {
this.context.globalAlpha = 1;
this.context.arc(this.canvasWidth * 0.5,
this.canvasHeight * 0.5, 50, 0, 2 * Math.PI, false);
this.context.lineWidth = 4;
this.context.strokeStyle = '#24B1FF';
this.context.stroke();
this.context.restore();
}
}
}
} window.customElements.define('blox-avatar', BloxAvatar);