-
Notifications
You must be signed in to change notification settings - Fork 78
/
index.html
397 lines (369 loc) · 14.1 KB
/
index.html
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
<html>
<head>
<title>PixelCraft: A Pixel Art Editor</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta property="og:locale" content="en_US" />
<meta property="og:title" content="PixelCraft: A Pixel Art Editor" />
<meta property="og:description" content="A pixel Art & Animation Creation Tool Built using HTML5 Canvas. It is a Progressive Web App (PWA) with offline compatibility. It is mobile-friendly and is very easy to use." />
<meta name="keywords" content="PixelCraft, pixel-art" />
<meta name="news_keywords" content="PixelCraft, pixel-art" />
<meta name="language" content="English">
<meta property="og:url" content="https://rgab1508.github.io/PixelCraft/" />
<meta property="og:site_name" content="PixelCraft" />
<meta property="article:tag" content="PixelCraft" />
<meta property="article:tag" content="pixel-art" />
<meta property="article:published_time" content="2020-04-22T08:34:48+00:00" />
<meta property="article:modified_time" content="2020-04-22T08:34:51+00:00" />
<meta property="og:updated_time" content="2020-04-22T08:34:51+00:00" />
<meta property="og:image" content="https://rgab1508.github.io/PixelCraft/images/overview.png" />
<meta property="og:image:secure_url" content="https://rgab1508.github.io/PixelCraft/images/overview.png" />
<meta property="og:image:width" content="600" />
<meta property="og:image:height" content="445" />
<meta name="twitter:card" content="summary_large_image" />
<meta name="twitter:description" content="A pixel Art & Animation Creation Tool Built using HTML5 Canvas. It is a Progressive Web App (PWA) with offline compatibility. It is mobile-friendly and is very easy to use." />
<meta name="twitter:title" content="PixelCraft: A Pixel Art Editor" />
<meta name="twitter:image" content="https://rgab1508.github.io/PixelCraft/images/overview.png" />
<link rel="shortcut icon" type="image/png" href="icons/logo.png"/>
<link rel="stylesheet" type="text/css" href="global.css" />
<link rel="manifest" href="manifest.json">
<script src="https://kit.fontawesome.com/473e8f3a80.js" crossorigin="anonymous"></script>
<script src="lib/Matrix.js" async></script>
<script src="lib/Shapes.js" async></script>
<script src="lib/Transformation.js" async></script>
<script src="lib/gif.js"></script>
<script src="Canvas.js"></script>
</head>
<body>
<div class="menubtn">☰</div>
<ul class="menu">
<li onclick="newProject()">New</li>
<li onclick="board.save()">Save image</li>
<li onclick="board.renderGIF()">Save GIF</li>
<li onclick="showColorSettings()">Color Settings</li>
<li id="install-pwa-btn" class="display-none" onclick="install()">Install PWA</li>
<li><a href="https://github.com/rgab1508/PixelCraft" target="_blank"><i class="fab fa-github"></i>Github</a></li>
</ul>
<div id="popup">
<h3>Select the Dimensions Of the grid</h3>
<input type="text" id="width" value="16">X<input type="text" id="height" value="16">
<button id="close">OK</button>
</div>
<div id="frames" onblur="Frames.close()" tabindex="0">
<div class="btn" style="left: 10px;" onclick="document.querySelector('#frames #gallery').scrollLeft-=100;"><i class="fa fa-chevron-left"></i></div>
<div class="btn" style="right: 10px;" onclick="document.querySelector('#frames #gallery').scrollLeft+=100;"><i class="fa fa-chevron-right"></i></div>
<div id="gallery"></div>
</div>
</div>
<canvas id="canvas"></canvas>
<div id="toolbar">
<span class="item" onclick="board.setmode(0)" style="background-color: grey;"><i class="fas fa-pencil-alt"></i></span>
<span class="item" onclick="board.setmode(1)"><i class="fas fa-eraser"></i></span>
<span class="item" onclick="board.setmode(2)"><i class="fas fa-fill"></i></span>
<span class="item" onclick="board.setmode(3)"><i class="fas fa-slash"></i></span>
<span class="item" onclick="board.setmode(4)"><i class="far fa-circle"></i></span>
<span class="item" onclick="board.setmode(5)"><i class="far fa-circle" style="transform: rotateX(45deg);"></i></span>
<span class="item" onclick="board.addFrame()"><i class="fas fa-plus"></i></span>
<span class="item" onclick="board.undo()"><i class="fas fa-undo"></i></span>
<span class="item" onclick="board.redo()"><i class="fas fa-redo"></i></span>
<span class="item" onclick="board.clear()"><i class="fas fa-trash"></i></span>
<span class="item" onclick="board.addImage()"><i class="fa fa-upload"></i></span>
<span class="item" onclick="Frames.open()"><i class="fas fa-eye"></i></span>
</div>
<div id="palette"></div>
<div id="color-settings-popup" style="display: none;">
<span onclick="document.getElementById('color-settings-popup').style.display = 'none';" class="close"><i class="fas fa-times" ></i></span>
<div class="color-palette"></div>
<div class="button-grp">
<button>Load Colors</button>
<button onclick="exportColors()">Export Colors</button>
<button>Save palette</button>
</div>
</div>
</body>
<script type="text/javascript">
// override of finishRendering of gif.js
GIF.prototype.finishRendering = function() {
var data, frame, i, image, j, k, l, len, len1, len2, len3, offset, page, ref, ref1, ref2;
len = 0;
ref = this.imageParts;
for (j = 0,
len1 = ref.length; j < len1; j++) {
frame = ref[j];
len += (frame.data.length - 1) * frame.pageSize + frame.cursor
}
len += frame.pageSize - frame.cursor;
this.log("rendering finished - filesize " + Math.round(len / 1e3) + "kb");
data = new Uint8Array(len);
offset = 0;
ref1 = this.imageParts;
for (k = 0,
len2 = ref1.length; k < len2; k++) {
frame = ref1[k];
ref2 = frame.data;
for (i = l = 0,
len3 = ref2.length; l < len3; i = ++l) {
page = ref2[i];
data.set(page, offset);
if (i === frame.data.length - 1) {
offset += frame.cursor
} else {
offset += frame.pageSize
}
}
}
image = new Blob([data],{
type: "image/gif"
});
this.running = false; // *this is the new part*
return this.emit("finished", image, data)
};
var Tool = {
"pen": 0,
"eraser": 1,
"fillBucket": 2,
"line": 3,
"circle": 4,
"ellipse": 5,
"addFrame": 6,
"undo": 7,
"redo": 8,
"clearCanvas": 9
};
var tools = [true, false, false, false, false, false];
var lc = [];
class Popup {
constructor(s) {
this.s = s;
document.querySelector(this.s).style.display = "block";
document.querySelector(this.s).style.transform = "translate(-50%,-50%) scale(1,1)";
}
close() {
document.querySelector(this.s).style.transform = "translate(-50%,-50%) scale(0,0)";
}
}
class Frames {
static open() {
document.querySelector("#frames").style.display = "block";
document.querySelector("#frames").style.transform = "translate(-50%,-50%) scale(1,1)";
document.querySelector("#frames").focus();
document.querySelector("#frames #gallery").innerHTML="";
for (var frame of board.frames) document.querySelector("#frames #gallery").appendChild(frame[0]);
document.querySelectorAll("#frames #gallery img").forEach((x,i) => {
x.onclick = (e) => {
board.loadFrame(i);
Frames.close();
};
x.oncontextmenu = (e) => {
e.preventDefault();
var del_confirmation = confirm("Delete?");
if (del_confirmation) {
board.deleteFrame(i);
Frames.open();
}
};
});
}
static close() {
document.querySelector("#frames").style.transform = "translate(-50%,-50%) scale(0,0)";
}
}
function createPalette (ele) {
ele.innerHTML = window.colors.map(x => (
`<div class="item" style="background-color: rgb(${x[0]},${x[1]},${x[2]})" ></div>`
)).join("\n");
}
function showColorSettings (e) {
document.querySelector("#color-settings-popup").style.display = "block";
createPalette(document.querySelector('#color-settings-popup > div.color-palette'))
}
function exportColors() {
let csvContent = "data:text/csv;charset=utf-8," ;
csvContent += window.colors.join("\n");
console.log(csvContent)
let encodedUri = encodeURI(csvContent);
let link = document.createElement("a");
link.setAttribute("href", encodedUri);
link.setAttribute("download", "colors.csv");
document.body.appendChild(link);
link.click();
}
window.onload = function () {
let canvasData = localStorage.getItem('pc-canvas-data');
if(canvasData){
data = JSON.parse(canvasData);
// console.log(data);
window.colors = data.colors;
if(window.board == undefined){
window.board = new Canvas(data.width, data.height);
}
let img = new Image();
img.setAttribute('src', data.url);
img.addEventListener("load", function () {
window.board.ctx.drawImage(img, 0, 0);
});
/*
window.board.frames = JSON.parse(data.frames).map(frame=>{
let img = new Image();
img.src = frame[0]
return [img, frame[1]]
});
for(let f in data.frames){
let c = document.createElement('canvas');
c.width = data.width;
c.height = data.height;
let c_ctx = c.getContext('2d');
c_ctx.drawImage(f[0], 0, 0);
window.board.addFrame(c.toDataURL());
}
*/
window.board.steps = data.steps;
window.board.redo_arr = data.redo_arr;
window.board.setcolor(data.currColor);
window.gif = new GIF({
workers: 2,
quality: 10,
width: 10 * window.board.width,
height: 10 * window.board.height
});
window.gif.on('finished', function (blob) {
var url = URL.createObjectURL(blob);
var link = document.createElement('a');
link.download = 'canvas.gif';
link.href = url;
link.click();
});
}
else {
newProject();
}
let paletteDiv = document.querySelector("#palette");
paletteDiv.innerHTML = colors.map(x =>
`<div class="item" style="background-color: rgb(${x[0]},${x[1]},${x[2]})" onclick="board.setcolor([${x}]);act(this);" oncontextmenu="board.setcolor([${x}]);act(this);board.ctx.globalAlpha=+prompt('Transparency(0-1)?')"></div>`
).join("\n");
// paletteDiv.innerHTML += '<div class="btn" ><i class="fa fa-upload"></i></div>';
document.querySelector("#palette").addEventListener("contextmenu",e=>e.preventDefault());
}
document.querySelector("#close").onclick = function () {
var width = +document.querySelector("#width").value;
var height = +document.querySelector("#height").value;
if(window.board == undefined){
window.board = new Canvas(width, height);
}
window.board.canvas.width = 10 * width;//display each pixel in 10 by 10pxs
window.board.canvas.height = 10 * height;
window.board.width = width;//Dimentions of x pixels
window.board.height = height;//Dimentions of Y pixels
window.board.canvas.style.display = "block";
window.board.canvas.style.height = Math.floor((height / width) * window.board.canvas.clientWidth) + "px";
window.board.w = +window.board.canvas.width;
window.board.h = +window.board.canvas.height;
window.board.ctx = window.board.canvas.getContext("2d");
window.board.ctx.fillStyle = "white";
window.board.ctx.globalAlpha = 1;
window.board.ctx.fillRect(0, 0, window.board.w, window.board.h);
window.board.data = [...Array(window.board.width)].map(e => Array(window.board.height).fill([255, 255, 255, 255]));
window.board.steps = [];
window.board.redo_arr = [];
window.board.frames = [];
window.board.setcolor([0, 0, 0, 255]);
window.dim.close();
window.gif = new GIF({
workers: 2,
quality: 10,
width: 10 * window.board.width,
height: 10 * window.board.height
});
window.gif.on('finished', function (blob) {
var url = URL.createObjectURL(blob);
var link = document.createElement('a');
link.download = 'canvas.gif';
link.href = url;
link.click();
});
}
document.querySelector(".menubtn").onclick = function () {
document.querySelector(".menu").style.display = document.querySelector(".menu").style.display != "block" ? "block" : "none";
}
function newProject(){
document.querySelector(".menu").style.display = "none";
localStorage.removeItem('pc-canvas-data');
window.dim = new Popup("#popup");
window.colors = [
[0, 0, 0, 255],
[127, 127, 127, 255],
[136, 0, 21, 255],
[237, 28, 36, 255],
[255, 127, 39, 255],
[255, 242, 0, 255],
[34, 177, 36, 255],
[0, 162, 232, 255],
[63, 72, 204, 255],
[163, 73, 164, 255],
[255, 255, 255, 255],
[195, 195, 195, 255],
[185, 122, 87, 255],
[255, 174, 201, 255],
[255, 201, 14, 255],
[239, 228, 176, 255],
[181, 230, 29, 255],
[153, 217, 234, 255],
[112, 146, 190, 255],
[200, 191, 231, 255]
];
}
function filler(x, y, cc) {
if (x >= 0 && x < board.width && y >= 0 && y < board.height) {
if (JSON.stringify(board.data[x][y]) == JSON.stringify(cc) && JSON.stringify(board.data[x][y]) != JSON.stringify(board.color)) {
board.draw(x, y);
filler(x + 1, y, cc);
filler(x, y + 1, cc);
filler(x - 1, y, cc);
filler(x, y - 1, cc);
}
}
}
function act(clr) {
document.querySelectorAll("#palette .item").forEach(x => {x.style.boxShadow = ""; x.style.border = "";});
clr.style.boxShadow = "10px 10px 10px 10px rgba(0,0,0,0.5)";
clr.style.border = `1px white solid`;
}
// window.onbeforeunload = function () {
// board.saveInLocal();
// return "Data will be lost if you leave the page, are you sure?";
// };
var scope = {
scope: './'
};
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register(
'sw.js',
scope
).then(function(serviceWorker) {
console.log('successful');
}).catch(function(error) {
alert("error");
});
} else {
console.log('unavailable');
}
var msg;
window.addEventListener('beforeinstallprompt', (e) => {
e.preventDefault();
msg = e;
// Shows the install button only when the app is installable
document.querySelector("#install-pwa-btn").classList.remove("display-none");
});
function install() {
msg.prompt();
msg.userChoice.then((choiceResult) => {
if (choiceResult.outcome === 'accepted') {
// Hides the install button
document.querySelector("#install-pwa-btn").classList.add("display-none");
}
});
}
window.onerror = function (errorMsg, url, lineNumber) {
alert('Error: ' + errorMsg + ' Script: ' + url + ' Line: ' + lineNumber);
}
</script>
</html>