-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditor.html
More file actions
334 lines (281 loc) · 8.57 KB
/
editor.html
File metadata and controls
334 lines (281 loc) · 8.57 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
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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title></title>
<script type="text/javascript" src="./jquery.min.js"></script>
<script type="text/javascript">
function getLimitedFunction(obj, func, freqPerSecond) {
if (typeof func !== "function")
throw new Error("Is not function in argument")
var previousCheckTime = new Date().valueOf()
var currentSecondCount = 0
return function decoratedFunction() {
var currentTime = new Date().valueOf()
if (currentTime - previousCheckTime > 100) {
previousCheckTime = currentTime
currentSecondCount = 0
}else {
if (currentSecondCount >= freqPerSecond)
return
}
currentSecondCount++
func.apply(obj, arguments)
}
}
function Block(x,y,width,height,name)
{
this.x=x;
this.y=y;
this.width=width;
this.height=height;
this.name=name;
}
this.mainLoop=getLimitedFunction(this,function() {
clearCanvas();
drawGrid();
drawBlocks();
drawNewBlock();
},3)
var mousePressed=false;
var canvas;
var context;
var blocks=[];
var blockCandidate;
var startCreatedBlockCoord;
var fullBlockWidth;
var fullBlockHeight;
var blockSpacerWidth;
var blockSpacerHeight;
function drawBlocks() {
for (var i=0; i < blocks.length; i++) {
var block=blocks[i];
drawBlock(block);
};
}
function drawBlock(block)
{
context.globalAlpha = 0.2;
context.fillStyle="#aae";
context.fillRect(block.x*fullBlockWidth+blockSpacerWidth/2,block.y*fullBlockHeight,block.width*fullBlockWidth-blockSpacerWidth,block.height*fullBlockHeight-blockSpacerHeight);
context.globalAlpha = 0.5;
context.strokeStyle="#000";
context.strokeRect(block.x*fullBlockWidth+blockSpacerWidth/2,block.y*fullBlockHeight,block.width*fullBlockWidth-blockSpacerWidth,block.height*fullBlockHeight-blockSpacerHeight);
context.globalAlpha = 0.8;
context.font = "bold 12px sans-serif";
context.fillText("#blockName",block.x*fullBlockWidth+blockSpacerWidth/2+2,block.y*fullBlockHeight+block.height*fullBlockHeight-blockSpacerHeight-1 );
}
function drawNewBlock() {
if(!blockCandidate) return;
context.globalAlpha = 0.1;
context.fillStyle="#aae";
context.fillRect(blockCandidate.x*fullBlockWidth+blockSpacerWidth/2,blockCandidate.y*fullBlockHeight,blockCandidate.width*fullBlockWidth-blockSpacerWidth,blockCandidate.height*fullBlockHeight-blockSpacerHeight);
}
function parseParam(val)
{
var typeParseRegExp=/([0-9]*)(em|pt|px)?/g;
var parsedInput=typeParseRegExp.exec(val);
var val=parsedInput[1];
var dim=parsedInput[2];
if(!dim)
{
return parseFloat(val);
}else {
return convert(val,dim,"px");
}
}
function readParams()
{
var nameRegExp=/(width|height)+/g
var res={};
var childrens=$("#paramForm>input, #paramForm>div>input");
for(var i=0;i<childrens.length;i++)
{
if(nameRegExp.test(childrens[i].id.toLowerCase()))
{
res[childrens[i].id]=parseParam(childrens[i].value);
}else {
res[childrens[i].id]=parseFloat(childrens[i].value);
}
}
return res;
}
function convert(val,from,to)
{
if(!val)
return 0;
var convertMtply={
ptToPx:1.5,
pxToEm: 1/16,
ptToEm: 1/12
};
from=from.toLowerCase();
to=to.toLowerCase();
if(from==to)
return parseFloat(val);
if(from!="pt" && from!="em" && from!="px")
throw new Error("error value type");
if(to!="pt" && to!="em" && to!="px")
throw new Error("error value type");
var p1=from;
var p2=to;
var param=p1+"To"+p2[0].toUpperCase()+p2.slice(1);
var mltply=1;
if(convertMtply[param])
mltply=convertMtply[param];
else {
param=p2+"To"+p1[0].toUpperCase()+p1.slice(1);
if(convertMtply[param])
mltply=1/convertMtply[param];
else
throw new Error("not found this convert value");
}
return parseFloat(val)*mltply;
}
function drawGrid()
{
context.lineWidth=1;
context.strokeStyle = "#aaa";
context.beginPath();
for(var y=0;y<paramObj.fullHeight;y=y+paramObj.fontHeight*paramObj.interlinearFactor)
{
context.moveTo(0,y);
context.lineTo(paramObj.fullWidth,y);
}
context.closePath();
context.stroke();
context.globalAlpha = 0.2;
context.fillStyle="#eaa";
for(var y=0;y<paramObj.fullHeight;y=y+paramObj.fontHeight*paramObj.interlinearFactor*(paramObj.horizBlockRowCount+1))
context.fillRect(0, y, paramObj.fullWidth, paramObj.fontHeight*paramObj.interlinearFactor*paramObj.horizBlockRowCount);
context.fillStyle="#faa";
for(var i=0;i<paramObj.verticalBlockCount;i++)
context.fillRect(i*(paramObj.verticalBlockWidth+paramObj.verticalBlockSpacerWidth)+blockSpacerWidth/2, 0,paramObj.verticalBlockWidth , paramObj.fullHeight);
}
function clearCanvas()
{
$("#mainCanvas")[0].width=paramObj.fullWidth;
$("#mainCanvas")[0].height=paramObj.fullHeight;
}
function updateBlockCandidate(pos)
{
var spacerWidth=(paramObj.verticalBlockWidth+paramObj.verticalBlockSpacerWidth);
var spacerHeight=(paramObj.fontHeight*paramObj.interlinearFactor*(paramObj.horizBlockRowCount+1) );
var xStartIdx=Math.floor(startCreatedBlockCoord.x/spacerWidth);
var yStartIdx=Math.floor(startCreatedBlockCoord.y/(paramObj.fontHeight*paramObj.interlinearFactor*(paramObj.horizBlockRowCount)));
var xIdx=Math.floor(pos.x/spacerWidth)+1;
var yIdx=Math.floor(pos.y/(paramObj.fontHeight*paramObj.interlinearFactor*(paramObj.horizBlockRowCount)))+1;
if(xStartIdx>xIdx)
{
var temp=xStartIdx;
xStartIdx=xIdx;
xIdx=temp;
}
if(yStartIdx>yIdx)
{
var temp=yStartIdx;
yStartIdx=yIdx;
yIdx=temp;
}
blockCandidate= new Block(xStartIdx,yStartIdx,xIdx-xStartIdx,yIdx-yStartIdx);
}
function checkNumCols()
{
var fullWidth=parseParam($("#fullWidth").val());
var blockCount=$("#verticalBlockCount").val();
var blockWidth=parseParam($("#verticalBlockWidth").val());
var spacerWidth=parseParam($("#verticalBlockSpacerWidth").val());
var diff=fullWidth-blockCount*(blockWidth+spacerWidth);
if(diff!=0)
{
fullWidth=fullWidth-diff;
}
$("#fullWidth").val(fullWidth);
}
function keyUpOnForm()
{
checkNumCols();
readFromForm();
$("#rowHeight").html(paramObj.fontHeight*paramObj.interlinearFactor);
}
function readFromForm()
{
canvas=$("#mainCanvas").get(0);
context=canvas.getContext("2d");
window.paramObj=readParams();
fullBlockWidth=paramObj.verticalBlockWidth+paramObj.verticalBlockSpacerWidth;
fullBlockHeight=paramObj.fontHeight*paramObj.interlinearFactor*(paramObj.horizBlockRowCount+1);
blockSpacerWidth=paramObj.verticalBlockSpacerWidth;
blockSpacerHeight=paramObj.fontHeight*paramObj.interlinearFactor;
}
function mouseDownCreate(pos)
{
startCreatedBlockCoord=pos;
updateBlockCandidate(pos);
}
function mouseUpCreate(pos)
{
updateBlockCandidate(pos);
if(blockCandidate)
blocks.push(blockCandidate);
blockCandidate=null;
mousePressed=false;
mainLoop();
}
function mouseMoveCreate(pos)
{
updateBlockCandidate(pos);
mainLoop();
}
$(document).ready(function() {
$("#do").click(function() {
readFromForm();
blocks=[];
mainLoop();
});
$("#mainCanvas").mousedown(function(e) {
var x = e.pageX - this.offsetLeft;
var y = e.pageY - this.offsetTop;
mouseDownCreate({x:x,y:y});
mousePressed=true;
});
$("#mainCanvas").mouseup(function(e) {
var x = e.pageX - this.offsetLeft;
var y = e.pageY - this.offsetTop;
mouseUpCreate({x:x,y:y});
});
$("#mainCanvas").mousemove(function(e) {
if(mousePressed) {
var x = e.pageX - this.offsetLeft;
var y = e.pageY - this.offsetTop;
mouseMoveCreate({x:x,y:y});
}
});
$("#paramForm>input, #paramForm>div>input").keyup(keyUpOnForm);
});
</script>
<style>
.verticalEditForm {
border: 1px solid gray;
}
</style>
</head>
<body>
<div id="paramForm">
Общая ширина: <input type="text" id="fullWidth" value="960px"></input><br>
Общая высота: <input type="text" id="fullHeight" value="2000px"></input><br>
Высота шрифта: <input type="text" id="fontHeight" value="12pt"></input><br>
Междустрочный интервал: <input type="text" id="interlinearFactor" value="1.5"></input><br>
Высота строки: <span id="rowHeight"></span><br>
Строк в горизонтальном блоке: <input type="text" id="horizBlockRowCount" value="3"></input><br>
<div class="verticalEditForm">
Количество вертикальных блоков: <input type="text" id="verticalBlockCount" value="12"></input><br>
Ширина вертикального блока: <input type="text" id="verticalBlockWidth" value="60px"></input><br>
Расстояние между блоками: <input type="text" id="verticalBlockSpacerWidth" value="20px"></input><br>
</div>
<button id="do">Создать канвас</button>
</div>
<canvas id="mainCanvas" style="border: 1px solid;">
</canvas>
</body>
</html>