-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
205 lines (176 loc) · 6.53 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
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
var vapeColors =
[
"Gold",
"Cyan",
"Grey",
"White",
"Black",
"Red",
"Orange",
"Blue",
"Burgundy"
];
var woodColors =
[
"None",
"Sun Yellow",
"Azo Orange",
"Primary Cyan",
"Permanent Red Violet",
"Permanent Blue Violet",
"Permanent Green",
"Marine Blue",
"Sap Green",
"Oxide Black"
];
var maxImageResolution = 1048;
var vapeColorImages = new Array();
var woodColorImages = new Array();
var totalImageCount = 22;
var loadedImageCount = 0;
var readyToDraw = false;
function generateResult() {
if (readyToDraw) {
var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');
context.clearRect(0, 0, canvas.width, canvas.height);
context.drawImage(currentVapeImage, 0, 0, canvas.width, canvas.height);
context.drawImage(currentWoodImage, 0, 0, canvas.width, canvas.height);
context.drawImage(currentStemImage, 0, 0, canvas.width, canvas.height);
}
}
function fillImageArray(imageArray) {
for (var i = 0; i < imageArray.length; i++) {
imageArray[i] = new Image();
}
}
function initImage(image, path) {
image.onload = function() {
loadedImageCount++;
if (loadedImageCount == totalImageCount) {
readyToDraw = true;
generateResult();
}
};
image.src = path;
}
function loadResources() {
woodTypeImages = new Array(2);
stemTypeImages = new Array(2);
vapeColorImages = new Array(9);
woodColorImages = new Array(10);
fillImageArray(woodTypeImages);
fillImageArray(stemTypeImages);
fillImageArray(vapeColorImages);
fillImageArray(woodColorImages);
initImage(woodTypeImages[0], 'Images/Beech.png');
initImage(woodTypeImages[1], 'Images/Poplar.png');
initImage(stemTypeImages[0], 'Images/Stem/Short.png');
initImage(stemTypeImages[1], 'Images/Stem/LongCurved.png');
initImage(vapeColorImages[0], 'Images/Vape/Gold.png');
initImage(vapeColorImages[1], 'Images/Vape/Cyan.png');
initImage(vapeColorImages[2], 'Images/Vape/Grey.png');
initImage(vapeColorImages[3], 'Images/Vape/White.png');
initImage(vapeColorImages[4], 'Images/Vape/Black.png');
initImage(vapeColorImages[5], 'Images/Vape/Red.png');
initImage(vapeColorImages[6], 'Images/Vape/Orange.png');
initImage(vapeColorImages[7], 'Images/Vape/Blue.png');
initImage(vapeColorImages[8], 'Images/Vape/Burgundy.png');
woodColorImages[0] = woodTypeImages[0];
initImage(woodColorImages[1], 'Images/Wood/SunYellow.png');
initImage(woodColorImages[2], 'Images/Wood/AzoOrange.png');
initImage(woodColorImages[3], 'Images/Wood/PrimaryCyan.png');
initImage(woodColorImages[4], 'Images/Wood/PermanentRedViolet.png');
initImage(woodColorImages[5], 'Images/Wood/PermanentBlueViolet.png');
initImage(woodColorImages[6], 'Images/Wood/PermanentGreen.png');
initImage(woodColorImages[7], 'Images/Wood/MarineBlue.png');
initImage(woodColorImages[8], 'Images/Wood/SapGreen.png');
initImage(woodColorImages[9], 'Images/Wood/OxideBlack.png');
currentStemImage = stemTypeImages[0];
currentVapeImage = vapeColorImages[0];
currentWoodImage = woodColorImages[0];
}
function updateWood(index) {
if (currentWoodImage === woodColorImages[0]) {
currentWoodImage = woodTypeImages[index];
}
woodColorImages[0] = woodTypeImages[index];
generateResult();
}
function updateStem(index) {
currentStemImage = stemTypeImages[index];
generateResult();
}
function resizeCanvas() {
var canvas = document.getElementById('canvas');
canvas.width = Math.min(window.innerHeight / 2, maxImageResolution);
canvas.height = Math.min(window.innerHeight / 2, maxImageResolution);
generateResult();
}
function isInt(value) {
return !isNaN(value) && parseInt(Number(value)) == value && !isNaN(parseInt(value, 10));
}
function checkRange(value, min, max) {
if (!isInt(value) || value < min || value > max) {
value = 0;
}
return value;
}
$(document).ready(function() {
resizeCanvas();
window.addEventListener('resize', resizeCanvas);
loadResources();
// Initialize text value from slider position
$('#vapeColorLabel').text(vapeColors[$('#vapeColorSlider').val()]);
$('#woodColorLabel').text(woodColors[$('#woodColorSlider').val()]);
// Add event handler to set text value when the sliders are dragged
$('#vapeColorSlider').on('input change', function () {
$('#vapeColorLabel').text(vapeColors[$(this).val()]);
currentVapeImage = vapeColorImages[$(this).val()];
generateResult();
});
$('#woodColorSlider').on('input change', function () {
$('#woodColorLabel').text(woodColors[$(this).val()]);
currentWoodImage = woodColorImages[$(this).val()];
generateResult();
});
// Set value of controls from URL parameters (if any)
var urlParams = new URLSearchParams(window.location.search);
var woodType = checkRange(urlParams.get('wood'), 0, 1);
var stemType = checkRange(urlParams.get('stem'), 0, 1);
var vapeColor = checkRange(urlParams.get('vcolor'), 0, 8);
var woodColor = checkRange(urlParams.get('wcolor'), 0, 9);
$('#woodTypes').children().eq(woodType).prop('checked', true);
$('#stemTypes').children().eq(stemType).prop('checked', true);
$('#vapeColorSlider').val(vapeColor);
$('#woodColorSlider').val(woodColor);
// Trigger changes
updateWood(woodType);
updateStem(stemType);
$('#vapeColorSlider').change();
$('#woodColorSlider').change();
});
function copyToClipboard(text) {
var input = document.createElement('input');
input.setAttribute('value', text);
document.body.appendChild(input);
input.select();
var result = document.execCommand('copy');
document.body.removeChild(input)
return result;
}
function generateURL() {
var woodTypeIndex = woodTypeImages.indexOf(woodColorImages[0]);
var stemTypeIndex = stemTypeImages.indexOf(currentStemImage);
var vapeColorIndex = vapeColorImages.indexOf(currentVapeImage);
var woodColorIndex = woodColorImages.indexOf(currentWoodImage);
var baseURL = "tubo-evic-customizer.netlify.com/";
var parameters = "?wood=" + woodTypeIndex + "&stem=" + stemTypeIndex + "&vcolor=" + vapeColorIndex + "&wcolor=" + woodColorIndex;
copyToClipboard(baseURL + parameters);
}
function downloadResult() {
var canvas = document.getElementById('canvas');
var img = canvas.toDataURL('image/png');
var tab = window.open('about:blank');
tab.document.write('<img src="' + img + '"/>');
}