-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdesigns.js
163 lines (141 loc) · 4.04 KB
/
designs.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
let status = 'draw'; // holds current state in relation to draw, erase, fill tools.
let col;
let row;
let isMouseDown = false;
let color;
const MAX_ALLOWED_HEIGHT = 30;
const MAX_ALLOWED_WIDTH = 30;
// makeGrid function to create pixel art grid
function makeGrid() {
row = $('#input_height').val();
col = $('#input_width').val();
$('#pixel_canvas').append(function() {
let newHtml = '';
row = Number(row);
col = Number(col);
for(let i = 0; i < row; i++){
newHtml += '<tr>';
for(let j = 0; j < col; j++){
newHtml += '<td></td>';
}
newHtml += '</tr>';
}
return newHtml;
});
};
// button event to create new grid
$('#size_picker').on('submit', function(e) {
e.preventDefault();
$('#pixel_canvas').empty();
makeGrid();
});
// click event to add / erase / get colour to/from grid elements,
// and also allowing to drag across multiple cells
$('#pixel_canvas').on('mousedown', 'td', function(e) {
e.preventDefault();
isMouseDown = true;
// *** this is starter code that can be uncommented and used when dropper 'getColour' function is added
// if(status === 'getColour'){
// let tempColour = $(this).css('background-color');
// $('input#color_picker').val(rgbToHex(tempColour));
// }
color = $('input#color_picker').val(); // ***May need to change this when colour palettes added
if(status === 'draw'){
$(this).css({ 'background-color': color });
} else if (status === 'erase'){
$(this).removeAttr('style');
} else if (status === 'fill'){
$('td').css({ 'background-color': color });
}
}).on('mouseover', 'td', function() {
if(isMouseDown && status === 'draw') {
$(this).css({ 'background-color': color });
} else if (isMouseDown && status === 'erase'){
$(this).removeAttr('style');
}
});
$(document).mouseup(function() {
isMouseDown = false;
});
// shortcut right-click event to remove colour from grid element
$('#pixel_canvas').on('contextmenu', 'td', function(e) {
e.preventDefault(); // stops context menu popping up on right-click
$(this).removeAttr('style');
});
// click border toggle button to toggle borders on the grid
$('.borderToggleBtn').on('click', function(){
$('tr, td').toggleClass('transparentBorder');
});
// function to clear the content of the canvas grid
$('.clearGrid').on('click', function(){
$('td').removeAttr('style');
});
// add Column to grid
$('.addCol').on('click', function(){
if(col < MAX_ALLOWED_WIDTH){
$('tr').append('<td></td>');
col++;
}
});
// remove Column from grid
$('.removeCol').on('click', function(){
if(col > 0){
$('tr td:last-child').remove();
col--;
}
});
// add Row to grid
$('.addRow').on('click', function(){
if(row < MAX_ALLOWED_HEIGHT){
row++;
let temp;
for (let k = 0; k < col; k++) {
temp += '<td></td>';
}
$('table').append('<tr>' + temp + '</tr>');
}
});
// remove Row from grid
$('.removeRow').on('click', function(){
if(row > 0){
$('tr:last-child').remove();
row--;
}
});
// Status button functions
$('.draw').click(function(){
status = 'draw';
});
$('.fill').click(function(){
status = 'fill';
});
$('.erase').click(function(){
status = 'erase';
});
// Function to handle Active tool styling
$('.tools').click(function(){
$('.tools').removeClass('active');
$(this).addClass('active');
});
// Export button function to save table canvas as .PNG file
$('.save').click(function(){
html2canvas($("#pixel_canvas").get(0), {
onrendered: function (canvas) {
var a = document.createElement('a');
a.href = canvas.toDataURL("image/png");
a.download = 'MyPixelArt.png';
a.click();
}
});
});
// TODO - add dropper function to get colour from a cell
// TODO - add colour palettes in right-hand column
// TODO - feature to share image on social media ???
// 'getColour' function
// $('.getColour').click(function(){
// status = 'getColour';
// ...
// });
// function to convert RGB colour value to Hex so it can be inserted
// as a value to the colour input
//function rgbToHex(){};