-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
167 lines (132 loc) · 3.8 KB
/
script.js
File metadata and controls
167 lines (132 loc) · 3.8 KB
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
let canvas, ctx, height, width, noOfRows, noOfColumns, preciseMouseClick = 13, fieldWidth = 90, fieldHeight = 30, cell,
topPadding = -15, leftPadding = 10;
let pos = [];
let alphabets = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','W','X','Y','Z'];
const input = document.getElementById("inputText");
const ovelay = document.getElementById("overlay");
let data = [];
// draw rows depending on height of canvas
const drawRows = () => {
let x = 0,y = fieldHeight;
for(let i = 0; i < (height / fieldHeight) - 1; i++){
ctx.moveTo(x,y);
ctx.lineTo(width,y);
ctx.strokeStyle = "#808080";
ctx.stroke();
y += fieldHeight;
}
}
// draw columns depending on width of canvas
const drawColoums = () => {
let x = fieldWidth,y = 0;
for(let i = 0; i < (width / fieldWidth) - 2; i++){
ctx.moveTo(x,y);
ctx.lineTo(x,width);
ctx.strokeStyle = "#808080";
ctx.stroke();
x += fieldWidth;
}
}
// draw text (A - Z) over column fields
const fillRowText = () => {
let x = fieldWidth, y = fieldHeight;
for(let i = 1; i < noOfColumns ; i++) {
ctx.font = "20px Georgia";
ctx.fillText(alphabets[i-1],x+90/2,y/2+5);
x += fieldWidth;
}
}
// draw numbers over row fields
const fillColumnText = () => {
let x = fieldWidth, y = fieldHeight;
for(let i = 1; i < noOfRows ; i++) {
ctx.font = "20px Georgia";
ctx.fillText(i,x/2,y+fieldHeight/2+5);
y += fieldHeight;
}
}
// get position of mouseclick to move input field
const getPosition = (event) =>
{
let x = event.x;
let y = event.y;
let canvas = document.getElementById("canvas");
// x -= canvas.offsetLeft;
// y -= canvas.offsetTop;
x -= preciseMouseClick;
y -= preciseMouseClick;
[x, y] = moveInputLocation(x,y);
cell = `${alphabets[x - 1]}${y}`;
if(data[cell] !== undefined) {
input.value = data[cell];
} else {
input.value = "";
}
pos = [x,y];
}
// implementation of moving input field over canvas
const moveInputLocation = (x,y) => {
input.style.display = "block";
x = Math.floor(x/fieldWidth);
y = Math.floor(y/fieldHeight);
overlay.style.left = x * fieldWidth + 'px';
overlay.style.top = y * fieldHeight + 'px';
return [x, y];
}
// store field data in array data structure
const storeData = () => {
data[cell] = input.value;
input.value = "";
input.style.display = "none";
}
// add item to data array and draw over canvas
const addItem = () => {
input.addEventListener('keyup', (event) => {
if(event.keyCode === 13) {
console.log(cell);
let [x , y] = pos;
storeData();
updateSheet();
}
});
}
const clearCanvas = () => {
ctx.clearRect(0 , 0, canvas.width, canvas.height);
}
// UI to draw data over spreadsheet
const updateSheet = () => {
clearCanvas();
drawRows();
drawColoums();
fillRowText();
fillColumnText();
//draw all data array item to sheet
const iterator = Object.keys(data);
for (const key of iterator) {
const x = alphabets.findIndex((cur) => cur == key.charAt(0)) + 1;
const y = key.match(/(\d+)/g);
ctx.font = "20px Georgia";
ctx.fillText(data[key],(x * fieldWidth) + fieldWidth / 3, (y * fieldHeight) + fieldHeight - (fieldHeight / 3));
}
}
// initialize the canvas with spreadsheet
const init = () =>
{
canvas = document.getElementById('canvas');
height = canvas.scrollHeight;
width = canvas.scrollWidth;
noOfColumns = width / fieldWidth;
noOfRows = height / fieldHeight;
canvas.style.border = "groove";
ctx = canvas.getContext('2d');
ctx.beginPath();
drawRows();
drawColoums();
fillRowText();
fillColumnText();
canvas.addEventListener("mousedown", getPosition, false);
input.style.height = `${fieldHeight - 3}px`;
input.style.width = `${fieldWidth - 5}px`;
addItem();
}
init();