-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrueba Linea.html
580 lines (544 loc) · 17.6 KB
/
Prueba Linea.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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
<!DOCTYPE html>
<html>
<head>
<title>Canvas Drawing Demo</title>
<style>
canvas {
border: 1px solid black;
display: block;
margin: 0 auto;
}
</style>
</head>
<body>
<canvas height="500" id="myCanvas" width="500"></canvas>
<div>
<label for="thickness">Line Thickness:</label>
<input id="thickness" max="10" min="1" type="range" value="5">
</div>
<div>
<label for="color1">Initial Color:</label>
<input id="color1" type="color" value="#000000">
</div>
<div>
<label for="color2">Final Color:</label>
<input id="color2" type="color" value="#ffffff">
</div>
<div>
<label for="lineType">Line Type:</label>
<select id="lineType">
<option value="solid">Solid</option>
<option value="dashed">Dashed</option>
<option value="dotted">Dotted</option>
</select>
</div>
<div>
<label for="label">Label:</label>
<input id="label" type="text" value="Label 1">
</div>
<script>
const x1 = 50;
const y1 = 50;
const x2 = 250;
const y2 = 250;
class DimensionDrawer {
constructor(canvasId) {
this.canvas = document.getElementById(canvasId);
this.ctx = this.canvas.getContext('2d');
this.thicknessInput = document.getElementById('thickness');
this.color1Input = document.getElementById('color1');
this.color2Input = document.getElementById('color2');
this.lineTypeSelect = document.getElementById('lineType');
this.label = document.getElementById('label');
this.draw();
// Add event listeners to update drawing when inputs change
this.thicknessInput.addEventListener('input', () => this.draw());
this.color1Input.addEventListener('input', () => this.draw());
this.color2Input.addEventListener('input', () => this.draw());
this.lineTypeSelect.addEventListener('input', () => this.draw());
this.label.addEventListener('input', () => this.draw());
}
draw() {
this.clearCanvas();
const text = this.label.value;
const thickness = this.thicknessInput.value;
const color1 = this.color1Input.value;
const color2 = this.color2Input.value;
const lineType = this.lineTypeSelect.value;
this.setLineStyle(thickness, color1, color2, lineType);
this.drawDimension(x1, y1, x2, y2, text);
}
clearCanvas() {
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
}
setLineStyle(thickness, color1, color2, lineType) {
const gradient = this.ctx.createLinearGradient(0, y1, 0, y2);
gradient.addColorStop(0, color1);
gradient.addColorStop(1, color2);
this.ctx.lineWidth = thickness;
this.ctx.strokeStyle = gradient;
if (lineType === 'dashed') {
this.ctx.setLineDash([10, 5]);
} else if (lineType === 'dotted') {
this.ctx.setLineDash([2, 5]);
} else {
this.ctx.setLineDash([]);
}
}
drawDimension(x1, y1, x2, y2, text) {
// Check input arguments
if (typeof x1 !== 'number' || typeof y1 !== 'number' ||
typeof x2 !== 'number' || typeof y2 !== 'number' ||
typeof text !== 'string') {
console.error('Invalid input arguments');
return;
}
// Calculate the distance and angle between the two points
const dx = x2 - x1;
const dy = y2 - y1;
const distance = Math.sqrt(dx * dx + dy * dy);
const angle = Math.atan2(dy, dx);
// Calculate the position of the text
const textWidth = this.ctx.measureText(text).width;
const textHeight = 12; // Assumed line height of 12 pixels
const textDistance = distance > textWidth + 20 ? 10 : -10 - textWidth;
// Draw the line and arrowhead
this.ctx.beginPath();
this.ctx.moveTo(x1, y1);
this.ctx.lineTo(x2, y2);
this.ctx.stroke();
this.drawArrowhead(x1, y1, angle);
this.drawArrowhead(x2, y2, angle - Math.PI);
// Draw the text
this.ctx.save();
this.ctx.font = '12px sans-serif';
this.ctx.textAlign = 'center';
this.ctx.translate((x1 + x2) / 2 + textDistance, (y1 + y2) / 2 - textHeight / 2);
this.ctx.rotate(angle);
this.ctx.fillText(text, 0, 0);
this.ctx.restore();
}
drawArrowhead(x, y, angle) {
this.ctx.save();
const arrowheadWidth = 10;
const arrowheadLength = 10;
// Draw the arrowheads
this.ctx.beginPath();
this.ctx.moveTo(x, y);
this.ctx.lineTo(
x + arrowheadLength * Math.cos(angle - Math.PI / 6),
y + arrowheadLength * Math.sin(angle - Math.PI / 6),
);
this.ctx.lineTo(
x + arrowheadWidth / 2 * Math.cos(angle),
y + arrowheadWidth / 2 * Math.sin(angle),
);
this.ctx.lineTo(
x + arrowheadLength * Math.cos(angle + Math.PI / 6),
y + arrowheadLength * Math.sin(angle + Math.PI / 6),
);
this.ctx.closePath();
this.ctx.fillStyle = this.ctx.strokeStyle;
this.ctx.fill();
// Restore the canvas state before drawing the second arrowhead
this.ctx.restore();
}
}
const drawer = new DimensionDrawer('myCanvas');
// Add animation to rotate the canvas
let angle = 0;
function rotateCanvas() {
angle += 0.12;
drawer.canvas.style.transform = `rotate(${angle}deg)`;
requestAnimationFrame(rotateCanvas);
}
rotateCanvas();
/*
Mentira, besitos mami no te conviértas en una camándula como abudan aquí:
Brothers me gustaría que me ayduaras a implementar:
La que faltan, cerca --x--x--x--x--x--x, onda de continuidad ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ algo así, etc...
Y armar un tipo de lina compleja donde puedas espeficar la escala del elemento ya sea tick, dot imagen o texto...
disfrútalo.
(c) 2023, Luis Guillermo Bultet Ibles (Carnet de identidad: 74092510328, BPA Account: 9204 1299 7686 6212
Se aceptan contribuciones.
// Algunos tipos especiales de líneas utilizadas en ingeniería y arquitectura
LINE_TYPES = {
SOLID: {
name: 'Solid',
dash: [],
},
// DASHED: {
// name: 'Dashed',
// dash: [10, 5],
// },
DOTTED: {
name: 'Dotted',
dash: [2, 5],
},
//
BORDER: {
name: 'Border',
dash: [0, 12.7, -6.35],
},
BORDER_2X: {
name: 'Border (2x)',
dash: [0, 25.4, -12.7],
},
BORDER_05X: {
name: 'Border (.5x)',
dash: [0, 6.35, -3.175],
},
CENTER: {
name: 'Center',
dash: [31.75, -6.35],
},
CENTER_2X: {
name: 'Center (2x)',
dash: [63.5, -12.7],
},
CENTER_05X: {
name: 'Center (.5x)',
dash: [19.05, -3.175],
},
DASH_DOT: {
name: 'Dash dot',
dash: [12.7, -6.35, 0, -6.35],
},
DASH_DOT_2X: {
name: 'Dash dot (2x)',
dash: [25.4, -12.7, 0, -12.7],
},
DASH_DOT_05X: {
name: 'Dash dot (.5x)',
dash: [6.35, -3.175, 0, -3.175],
},
DASHED: {
name: 'Dashed',
dash: [12.7, -6.35],
},
DASHED_2X: {
name: 'Dashed (2x)',
dash: [25.4, -12.7],
},
DASHED_05X: {
name: 'Dashed (.5x)',
dash: [6.35, -3.175],
},
DIVIDE: {
name: 'Divide',
dash: [12.7, -6.35, 0, -6.35],
},
DIVIDE_2X: {
name: 'Divide (2x)',
dash: [25.4, -12.7, 0, -12.7],
},
DIVIDE_05X: {
name: 'Divide (.5x)',
dash: [6.35, -3.175, 0, -3.175],
},
DOT: {
name: 'Dot',
dash: [0, 6.35],
},
DOT_2X: {
name: 'Dot (2x)',
dash: [0, 12.7],
},
DOT_05X: {
name: 'Dot (.5x)',
dash: [0, 3.175],
},
HIDDEN: {
name: 'Hidden',
dash: [0, 12.7],
},
HIDDEN_2X: {
name: 'Hidden (2x)',
dash: [0, 25.4],
},
HIDDEN_05X: {
name: 'Hidden (.5x)',
dash: [0, 6.35],
},
PHANTOM: {
name: 'Phantom',
dash: [12.7, 12.7],
},
PHANTOM_2X: {
name: 'Phantom (2x)',
dash: [25.4, 25.4],
},
PHANTOM_05X: {
name: 'Phantom (.5x)',
dash: [6.35, 6.35],
},
ISO_DASH: {
name: 'ISO dash',
dash: [30, -15],
},
ISO_DASH_SPACE: {
name: 'ISO dash space',
dash: [30, -15, 0, -15],
},
ISO_LONG_DASH_DOT: {
name: 'ISO long-dash dot',
dash: [90, -15, 0, -15],
},
ISO_LONG_DASH_DOUBLE_DOT: {
name: 'ISO long-dash double-dot',
dash: [90, -15, 0, -15, 0, -15],
},
ISO_LONG_DASH_TRIPLE_DOT: {
name: 'ISO long-dash triple-dot',
dash: [90, -15, 0, -15, 0, -15, 0, -15],
},
ISO_DOT: {
name: 'ISO dot',
dash: [0, 15],
},
ISO_LONG_DASH_SHORT_DASH: {
name: 'ISO long-dash short-dash',
dash: [60, -15, 0, -15, 0, -15],
},
ISO_LONG_DASH_DOUBLE_SHORT_DASH: {
name: 'ISO long-dash double-short-dash',
dash: [60, -15, 0, -15, 0, -15, 0, -15],
},
ISO_DASH_DOT: {
name: 'ISO dash dot',
dash: [30, -15, 0, -15, 0, -15],
},
ISO_DOUBLE_DASH_DOT: {
name: 'ISO double-dash dot',
dash: [60, -15, 0, -15, 0, -15, 0, -15],
},
ISO_DASH_DOUBLE_DOT: {
name: 'ISO dash double-dot',
dash: [30, -15, 0, -15, 0, -15, 0, -15, 0, -15],
},
ISO_DOUBLE_DASH_DOUBLE_DOT: {
name: 'ISO double-dash double-dot',
dash: [60, -15, 0, -15, 0, -15, 0, -15, 0, -15, 0, -15],
},
ISO_DASH_TRIPLE_DOT: {
name: 'ISO dash triple-dot',
dash: [30, -15, 0, -15, 0, -15, 0, -15, 0, -15, 0, -15],
},
ISO_DOUBLE_DASH_TRIPLE_DOT: {
name: 'ISO double-dash triple-dot',
dash: [60, -15, 0, -15, 0, -15, 0, -15, 0, -15, 0, -15, 0, -15],
},
};
// PARE DIBUJAR LINEAS CON O SIN GRADACIONES
line(x1, y1, x2, y2, startColor = this.options.stroke, endColor = this.options.stroke, lineWidth = this.options['stroke-width'], lineJoin = this.options.lineJoin, lineCap = this.options.lineCap, dash = [], lineType = '') {
this.ctx.lineWidth = lineWidth;
this.ctx.lineJoin = lineJoin;
this.ctx.lineCap = lineCap;
// Crear gradiente si se especifican colores de inicio y fin
if (endColor && (startColor !== endColor)) {
if (!this.gradientCache) {
this.gradientCache = {}; // Objeto para almacenar los gradientes reutilizables
}
const gradientId = `${startColor}-${endColor}`; // Identificador único para este gradiente
let grad = this.gradientCache[gradientId];
if (!grad) {
grad = this.ctx.createLinearGradient(x1, y1, x2, y2);
grad.addColorStop(0, startColor);
grad.addColorStop(1, endColor);
this.gradientCache[gradientId] = grad;
}
this.ctx.strokeStyle = grad;
} else {
this.ctx.strokeStyle = startColor;
}
// Seleccionar el patrón de línea en función del tipo de línea
if (dash) {
this.ctx.setLineDash(dash);
} else {
let gh = this.LINE_TYPES.find((lt) => lt.name === lineType);
if (gh) {
this.ctx.setLineDash(gh.dash);
} else {
switch (lineType) {
case 'Fenceline circle': {
const radius = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2)) / 2;
const centerX = (x1 + x2) / 2;
const centerY = (y1 + y2) / 2;
this.ctx.beginPath();
this.ctx.arc(centerX, centerY, radius, 0, 2 * Math.PI);
this.ctx.stroke();
break;
}
case 'Fenceline square': {
const sideLength = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
const centerX = (x1 + x2) / 2;
const centerY = (y1 + y2) / 2;
this.ctx.beginPath();
this.ctx.rect(centerX - sideLength / 2, centerY - sideLength / 2, sideLength, sideLength);
this.ctx.stroke();
break;
}
case 'Tracks': {
const length = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
const angle = Math.atan2(y2 - y1, x2 - x1);
this.ctx.save();
this.ctx.translate(x1, y1);
this.ctx.rotate(angle);
this.ctx.fillRect(0, -2.5, length, 5);
this.ctx.restore();
break;
}
case 'Batting': {
const length = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
const angle = Math.atan2(y2 - y1, x2 - x1);
this.ctx.save();
this.ctx.translate(x1, y1);
this.ctx.rotate(angle);
this.ctx.beginPath();
this.ctx.moveTo(0, -2.5);
this.ctx.lineTo(length / 2, -2.5);
this.ctx.lineTo(length / 2, -7.5);
this.ctx.lineTo(0, -7.5);
this.ctx.closePath();
this.ctx.stroke();
this.ctx.fill();
this.ctx.restore();
break;
}
case 'Hot water supply': {
const length = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
const angle = Math.atan2(y2 - y1, x2 - x1);
this.ctx.save();
this.ctx.translate(x1, y1);
this.ctx.rotate(angle);
this.ctx.beginPath();
this.ctx.moveTo(0, -2.5);
this.ctx.lineTo(length / 2, -2.5);
this.ctx.lineTo(length / 2, -7.5);
this.ctx.lineTo(0, -7.5);
this.ctx.closePath();
this.ctx.stroke();
this.ctx.beginPath();
this.ctx.moveTo(length / 2 - 2.5, -5);
this.ctx.lineTo(length / 2 + 2.5, -5);
this.ctx.lineTo(length / 2 + 2.5, -10);
this.ctx.lineTo(length / 2 - 2.5, -10);
this.ctx.closePath();
this.ctx.fill();
this.ctx.restore();
break;
}
case 'Gas line 1': {
const length = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
const angle = Math.atan2(y2 - y1, x2 - x1);
this.ctx.save();
this.ctx.translate(x1, y1);
this.ctx.rotate(angle);
this.ctx.beginPath();
this.ctx.moveTo(0, 0);
this.ctx.lineTo(length / 4, 0);
this.ctx.lineTo(length / 4, -5);
this.ctx.lineTo(length / 2, -5);
this.ctx.lineTo(length / 2, -10);
this.ctx.lineTo(3 * length / 4, -10);
this.ctx.lineTo(3 * length / 4, -5);
this.ctx.lineTo(length, -5);
this.ctx.lineTo(length, -10);
this.ctx.stroke();
this.ctx.restore();
break;
}
case 'Gas line 2': {// codeword galileo: tomar el modelo para generalizar textos en la línea
const length = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
const angle = Math.atan2(y2 - y1, x2 - x1);
this.ctx.save();
this.ctx.translate(x1, y1);
this.ctx.rotate(angle);
this.ctx.beginPath();
this.ctx.moveTo(0, -2.5);
this.ctx.lineTo(length / 2, -2.5);
this.ctx.lineTo(length / 2, -7.5);
this.ctx.lineTo(0, -7.5);
this.ctx.closePath();
this.ctx.stroke();
this.ctx.beginPath();
this.ctx.arc(length / 4, -5, 2.5, 0, 2 * Math.PI);
this.ctx.closePath();
this.ctx.fill();
this.ctx.fillText('GAS', length / 2 + 5, -5);
this.ctx.restore();
break;
}
case 'Zig zag': {
const length = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
const angle = Math.atan2(y2 - y1, x2 - x1);
this.ctx.save();
this.ctx.translate(x1, y1);
this.ctx.rotate(angle);
this.ctx.beginPath();
this.ctx.moveTo(0, 0);
this.ctx.lineTo(length / 4, 0);
this.ctx.lineTo(length / 4, -5);
this.ctx.lineTo(length / 2, -5);
this.ctx.lineTo(length / 2, -10);
this.ctx.lineTo(3 * length / 4, -10);
this.ctx.lineTo(3 * length / 4, -5);
this.ctx.lineTo(length, -5);
this.ctx.lineTo(length, -10);
this.ctx.stroke();
this.ctx.restore();
break;
}
case 'Drainage': {
const length = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
const angle = Math.atan2(y2 - y1, x2 - x1);
this.ctx.save();
this.ctx.translate(x1, y1);
this.ctx.rotate(angle);
this.ctx.beginPath();
this.ctx.moveTo(0, -2.5);
this.ctx.lineTo(length, -2.5);
this.ctx.lineTo(length, -7.5);
this.ctx.lineTo(0, -7.5);
this.ctx.closePath();
this.ctx.stroke();
this.ctx.beginPath();
this.ctx.moveTo(length / 2, -2.5);
this.ctx.lineTo(length / 2, -10);
this.ctx.stroke();
this.ctx.restore();
break;
}
case 'Drainage reversed': {
const length = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
const angle = Math.atan2(y2 - y1, x2 - x1);
this.ctx.save();
this.ctx.translate(x1, y1);
this.ctx.rotate(angle);
this.ctx.beginPath();
this.ctx.moveTo(0, -7.5);
this.ctx.lineTo(length, -7.5);
this.ctx.lineTo(length, -2.5);
this.ctx.lineTo(0, -2.5);
this.ctx.closePath();
this.ctx.stroke();
this.ctx.beginPath();
this.ctx.moveTo(length / 2, -2.5);
this.ctx.lineTo(length / 2, -10);
this.ctx.stroke();
this.ctx.restore();
break;
}
default: {
this.ctx.moveTo(Math.round(x1), Math.round(y1));
this.ctx.lineTo(Math.round(x2), Math.round(y2));
this.endPath();
}
}
}
}
}
*/
</script>
</body>
</html>