plotboilerplate 1.15.0 → 1.17.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -0
- package/changelog.md +65 -0
- package/dist/index.esm.js +760 -667
- package/dist/index.esm.js.map +1 -1
- package/dist/index.esm.min.js +1 -1
- package/dist/index.esm.min.js.map +1 -1
- package/dist/plotboilerplate.browser.min.js +1 -1
- package/dist/plotboilerplate.browser.min.js.map +1 -1
- package/dist/plotboilerplate.js +765 -571
- package/dist/plotboilerplate.min.js +1 -1
- package/package.json +1 -1
- package/src/cjs/BezierPath.d.ts +32 -14
- package/src/cjs/BezierPath.js +65 -24
- package/src/cjs/BezierPath.js.map +1 -1
- package/src/cjs/Bounds.d.ts +33 -1
- package/src/cjs/Bounds.js +38 -1
- package/src/cjs/Bounds.js.map +1 -1
- package/src/cjs/Circle.d.ts +15 -13
- package/src/cjs/Circle.js +16 -34
- package/src/cjs/Circle.js.map +1 -1
- package/src/cjs/CircleSector.d.ts +17 -16
- package/src/cjs/CircleSector.js +12 -24
- package/src/cjs/CircleSector.js.map +1 -1
- package/src/cjs/CubicBezierCurve.d.ts +16 -15
- package/src/cjs/CubicBezierCurve.js +57 -94
- package/src/cjs/CubicBezierCurve.js.map +1 -1
- package/src/cjs/KeyHandler.d.ts +10 -3
- package/src/cjs/KeyHandler.js +28 -23
- package/src/cjs/KeyHandler.js.map +1 -1
- package/src/cjs/Line.d.ts +2 -15
- package/src/cjs/Line.js +6 -31
- package/src/cjs/Line.js.map +1 -1
- package/src/cjs/MouseHandler.js +43 -53
- package/src/cjs/MouseHandler.js.map +1 -1
- package/src/cjs/PBImage.d.ts +15 -13
- package/src/cjs/PBImage.js +11 -15
- package/src/cjs/PBImage.js.map +1 -1
- package/src/cjs/PBText.d.ts +13 -12
- package/src/cjs/PBText.js +8 -12
- package/src/cjs/PBText.js.map +1 -1
- package/src/cjs/PlotBoilerplate.d.ts +3 -1
- package/src/cjs/PlotBoilerplate.js +9 -2
- package/src/cjs/PlotBoilerplate.js.map +1 -1
- package/src/cjs/Polygon.d.ts +23 -14
- package/src/cjs/Polygon.js +22 -35
- package/src/cjs/Polygon.js.map +1 -1
- package/src/cjs/Triangle.d.ts +15 -13
- package/src/cjs/Triangle.js +23 -64
- package/src/cjs/Triangle.js.map +1 -1
- package/src/cjs/VEllipse.d.ts +14 -9
- package/src/cjs/VEllipse.js +10 -18
- package/src/cjs/VEllipse.js.map +1 -1
- package/src/cjs/VEllipseSector.d.ts +15 -1
- package/src/cjs/VEllipseSector.js +11 -1
- package/src/cjs/VEllipseSector.js.map +1 -1
- package/src/cjs/Vector.d.ts +3 -15
- package/src/cjs/Vector.js +9 -49
- package/src/cjs/Vector.js.map +1 -1
- package/src/cjs/VertTuple.d.ts +15 -1
- package/src/cjs/VertTuple.js +14 -19
- package/src/cjs/VertTuple.js.map +1 -1
- package/src/cjs/Vertex.d.ts +23 -13
- package/src/cjs/Vertex.js +24 -22
- package/src/cjs/Vertex.js.map +1 -1
- package/src/cjs/VertexListeners.d.ts +12 -8
- package/src/cjs/VertexListeners.js +18 -25
- package/src/cjs/VertexListeners.js.map +1 -1
- package/src/cjs/draw.d.ts +45 -3
- package/src/cjs/draw.js +149 -6
- package/src/cjs/draw.js.map +1 -1
- package/src/cjs/drawgl.d.ts +41 -3
- package/src/cjs/drawgl.js +45 -3
- package/src/cjs/drawgl.js.map +1 -1
- package/src/cjs/drawutilssvg.d.ts +69 -4
- package/src/cjs/drawutilssvg.js +147 -16
- package/src/cjs/drawutilssvg.js.map +1 -1
- package/src/cjs/index.d.ts +0 -1
- package/src/cjs/index.js +0 -1
- package/src/cjs/index.js.map +1 -1
- package/src/cjs/interfaces/DrawLib.d.ts +64 -27
- package/src/cjs/interfaces/DrawLib.js +3 -0
- package/src/cjs/interfaces/DrawLib.js.map +1 -1
- package/src/cjs/interfaces/core.d.ts +2 -12
- package/src/cjs/interfaces/core.js +1 -0
- package/src/cjs/interfaces/core.js.map +1 -1
- package/src/cjs/module.d.ts +1 -3
- package/src/cjs/module.js +2 -5
- package/src/cjs/module.js.map +1 -1
- package/src/cjs/utils/creategui.js +16 -2
- package/src/cjs/utils/datastructures/Color.d.ts +3 -1
- package/src/cjs/utils/datastructures/Color.js +17 -6
- package/src/cjs/utils/datastructures/Color.js.map +1 -1
- package/src/cjs/utils/datastructures/Girih.d.ts +5 -0
- package/src/cjs/utils/datastructures/Girih.js +18 -14
- package/src/cjs/utils/datastructures/Girih.js.map +1 -1
- package/src/cjs/utils/datastructures/GirihPenroseRhombus.js +42 -25
- package/src/cjs/utils/datastructures/GirihPenroseRhombus.js.map +1 -1
- package/src/cjs/utils/datastructures/GirihTile.js +2 -13
- package/src/cjs/utils/datastructures/GirihTile.js.map +1 -1
- package/src/cjs/utils/helpers/BezierPathInteractionHelper.d.ts +17 -1
- package/src/cjs/utils/helpers/BezierPathInteractionHelper.js +50 -32
- package/src/cjs/utils/helpers/BezierPathInteractionHelper.js.map +1 -1
- package/src/cjs/utils/io/DndFileHandler.d.ts +63 -0
- package/src/cjs/utils/io/DndFileHandler.js +112 -0
- package/src/cjs/utils/io/DndFileHandler.js.map +1 -0
- package/src/cjs/utils/io/FileDrop.d.ts +98 -0
- package/src/cjs/utils/io/FileDrop.js +142 -0
- package/src/cjs/utils/io/FileDrop.js.map +1 -0
- package/src/esm/BezierPath.d.ts +32 -14
- package/src/esm/BezierPath.js +65 -24
- package/src/esm/BezierPath.js.map +1 -1
- package/src/esm/Bounds.d.ts +33 -1
- package/src/esm/Bounds.js +38 -1
- package/src/esm/Bounds.js.map +1 -1
- package/src/esm/Circle.d.ts +15 -13
- package/src/esm/Circle.js +16 -34
- package/src/esm/Circle.js.map +1 -1
- package/src/esm/CircleSector.d.ts +17 -16
- package/src/esm/CircleSector.js +12 -24
- package/src/esm/CircleSector.js.map +1 -1
- package/src/esm/CubicBezierCurve.d.ts +16 -15
- package/src/esm/CubicBezierCurve.js +57 -94
- package/src/esm/CubicBezierCurve.js.map +1 -1
- package/src/esm/KeyHandler.d.ts +10 -3
- package/src/esm/KeyHandler.js +28 -23
- package/src/esm/KeyHandler.js.map +1 -1
- package/src/esm/Line.d.ts +2 -15
- package/src/esm/Line.js +6 -31
- package/src/esm/Line.js.map +1 -1
- package/src/esm/MouseHandler.js +43 -53
- package/src/esm/MouseHandler.js.map +1 -1
- package/src/esm/PBImage.d.ts +15 -13
- package/src/esm/PBImage.js +11 -15
- package/src/esm/PBImage.js.map +1 -1
- package/src/esm/PBText.d.ts +13 -12
- package/src/esm/PBText.js +8 -12
- package/src/esm/PBText.js.map +1 -1
- package/src/esm/PlotBoilerplate.d.ts +3 -1
- package/src/esm/PlotBoilerplate.js +9 -2
- package/src/esm/PlotBoilerplate.js.map +1 -1
- package/src/esm/Polygon.d.ts +23 -14
- package/src/esm/Polygon.js +22 -35
- package/src/esm/Polygon.js.map +1 -1
- package/src/esm/Triangle.d.ts +15 -13
- package/src/esm/Triangle.js +23 -64
- package/src/esm/Triangle.js.map +1 -1
- package/src/esm/VEllipse.d.ts +14 -9
- package/src/esm/VEllipse.js +10 -18
- package/src/esm/VEllipse.js.map +1 -1
- package/src/esm/VEllipseSector.d.ts +15 -1
- package/src/esm/VEllipseSector.js +11 -1
- package/src/esm/VEllipseSector.js.map +1 -1
- package/src/esm/Vector.d.ts +3 -15
- package/src/esm/Vector.js +9 -49
- package/src/esm/Vector.js.map +1 -1
- package/src/esm/VertTuple.d.ts +15 -1
- package/src/esm/VertTuple.js +14 -19
- package/src/esm/VertTuple.js.map +1 -1
- package/src/esm/Vertex.d.ts +23 -13
- package/src/esm/Vertex.js +24 -22
- package/src/esm/Vertex.js.map +1 -1
- package/src/esm/VertexListeners.d.ts +12 -8
- package/src/esm/VertexListeners.js +18 -25
- package/src/esm/VertexListeners.js.map +1 -1
- package/src/esm/draw.d.ts +45 -3
- package/src/esm/draw.js +147 -6
- package/src/esm/draw.js.map +1 -1
- package/src/esm/drawgl.d.ts +41 -3
- package/src/esm/drawgl.js +44 -3
- package/src/esm/drawgl.js.map +1 -1
- package/src/esm/drawutilssvg.d.ts +69 -4
- package/src/esm/drawutilssvg.js +146 -16
- package/src/esm/drawutilssvg.js.map +1 -1
- package/src/esm/index.d.ts +0 -1
- package/src/esm/index.js +0 -1
- package/src/esm/index.js.map +1 -1
- package/src/esm/interfaces/DrawLib.d.ts +64 -27
- package/src/esm/interfaces/DrawLib.js +3 -0
- package/src/esm/interfaces/DrawLib.js.map +1 -1
- package/src/esm/interfaces/core.d.ts +2 -12
- package/src/esm/interfaces/core.js +1 -0
- package/src/esm/interfaces/core.js.map +1 -1
- package/src/esm/module.d.ts +1 -3
- package/src/esm/module.js +5 -4
- package/src/esm/module.js.map +1 -1
- package/src/esm/utils/datastructures/Color.d.ts +3 -1
- package/src/esm/utils/datastructures/Color.js +17 -6
- package/src/esm/utils/datastructures/Color.js.map +1 -1
- package/src/esm/utils/datastructures/Girih.d.ts +5 -0
- package/src/esm/utils/datastructures/Girih.js +18 -14
- package/src/esm/utils/datastructures/Girih.js.map +1 -1
- package/src/esm/utils/datastructures/GirihPenroseRhombus.js +42 -25
- package/src/esm/utils/datastructures/GirihPenroseRhombus.js.map +1 -1
- package/src/esm/utils/datastructures/GirihTile.js +2 -13
- package/src/esm/utils/datastructures/GirihTile.js.map +1 -1
- package/src/esm/utils/helpers/BezierPathInteractionHelper.d.ts +17 -1
- package/src/esm/utils/helpers/BezierPathInteractionHelper.js +49 -32
- package/src/esm/utils/helpers/BezierPathInteractionHelper.js.map +1 -1
- package/src/esm/utils/io/DndFileHandler.d.ts +63 -0
- package/src/esm/utils/io/DndFileHandler.js +106 -0
- package/src/esm/utils/io/DndFileHandler.js.map +1 -0
- package/src/esm/utils/io/FileDrop.d.ts +98 -0
- package/src/esm/utils/io/FileDrop.js +136 -0
- package/src/esm/utils/io/FileDrop.js.map +1 -0
- package/src/ts/BezierPath.ts +78 -22
- package/src/ts/Bounds.ts +40 -1
- package/src/ts/Circle.ts +187 -200
- package/src/ts/CircleSector.ts +141 -141
- package/src/ts/CubicBezierCurve.ts +738 -810
- package/src/ts/KeyHandler.ts +221 -212
- package/src/ts/Line.ts +48 -80
- package/src/ts/MouseHandler.ts +356 -368
- package/src/ts/PBImage.ts +67 -69
- package/src/ts/PBText.ts +16 -14
- package/src/ts/PlotBoilerplate.ts +9 -2
- package/src/ts/Polygon.ts +34 -34
- package/src/ts/Triangle.ts +444 -489
- package/src/ts/VEllipse.ts +18 -17
- package/src/ts/VEllipseSector.ts +20 -1
- package/src/ts/Vector.ts +124 -160
- package/src/ts/VertTuple.ts +333 -338
- package/src/ts/Vertex.ts +35 -21
- package/src/ts/VertexListeners.ts +248 -266
- package/src/ts/draw.ts +201 -6
- package/src/ts/drawgl.ts +47 -3
- package/src/ts/drawutilssvg.ts +187 -16
- package/src/ts/index.ts +0 -1
- package/src/ts/interfaces/DrawLib.ts +72 -27
- package/src/ts/interfaces/core.ts +3 -13
- package/src/ts/module.ts +2 -4
- package/src/ts/utils/datastructures/Color.ts +17 -6
- package/src/ts/utils/datastructures/Girih.ts +204 -209
- package/src/ts/utils/datastructures/GirihPenroseRhombus.ts +170 -152
- package/src/ts/utils/datastructures/GirihTile.ts +394 -419
- package/src/ts/utils/helpers/BezierPathInteractionHelper.ts +56 -34
- package/src/ts/utils/io/FileDrop.ts +178 -0
- package/src/ts/SVGBuilder.ts +0 -109
package/src/ts/draw.ts
CHANGED
|
@@ -39,7 +39,12 @@
|
|
|
39
39
|
* @modified 2021-05-31 Added the `setConfiguration` function from `DrawLib`.
|
|
40
40
|
* @modified 2021-11-12 Adding more parameters tot the `text()` function: fontSize, textAlign, fontFamily, lineHeight.
|
|
41
41
|
* @modified 2021-11-19 Added the `color` param to the `label(...)` function.
|
|
42
|
-
* @
|
|
42
|
+
* @modified 2022-02-03 Added the `lineWidth` param to the `crosshair` function.
|
|
43
|
+
* @modified 2022-02-03 Added the `cross(...)` function.
|
|
44
|
+
* @modified 2022-03-27 Added the `texturedPoly` function.
|
|
45
|
+
* @modified 2022-06-01 Tweaked the `polyline` function; lineWidth now scales with scale.x.
|
|
46
|
+
* @modified 2022-07-26 Adding `alpha` to the `image(...)` function.
|
|
47
|
+
* @version 1.12.2
|
|
43
48
|
**/
|
|
44
49
|
|
|
45
50
|
import { CubicBezierCurve } from "./CubicBezierCurve";
|
|
@@ -47,6 +52,7 @@ import { Polygon } from "./Polygon";
|
|
|
47
52
|
import { Vertex } from "./Vertex";
|
|
48
53
|
import { DrawLib, SVGPathParams, XYCoords, UID, DrawLibConfiguration, FontStyle, FontWeight } from "./interfaces";
|
|
49
54
|
import { drawutilssvg } from "./drawutilssvg";
|
|
55
|
+
import { Bounds } from "./Bounds";
|
|
50
56
|
|
|
51
57
|
// Todo: rename this class to Drawutils?
|
|
52
58
|
/**
|
|
@@ -225,16 +231,18 @@ export class drawutils implements DrawLib<void> {
|
|
|
225
231
|
* @param {Image} image - The image object to draw.
|
|
226
232
|
* @param {Vertex} position - The position to draw the the upper left corner at.
|
|
227
233
|
* @param {Vertex} size - The x/y-size to draw the image with.
|
|
234
|
+
* @param {number=0.0} alpha - (optional, default=0.0) The transparency (1.0=opaque, 0.0=transparent).
|
|
228
235
|
* @return {void}
|
|
229
236
|
* @instance
|
|
230
237
|
* @memberof drawutils
|
|
231
238
|
**/
|
|
232
|
-
image(image: HTMLImageElement, position: Vertex, size: Vertex): void {
|
|
239
|
+
image(image: HTMLImageElement, position: Vertex, size: Vertex, alpha: number = 1.0): void {
|
|
233
240
|
if (!image.complete || !image.naturalWidth) {
|
|
234
241
|
// Avoid drawing un-unloaded or broken images
|
|
235
242
|
return;
|
|
236
243
|
}
|
|
237
244
|
this.ctx.save();
|
|
245
|
+
this.ctx.globalAlpha = alpha;
|
|
238
246
|
// Note that there is a Safari bug with the 3 or 5 params variant.
|
|
239
247
|
// Only the 9-param varaint works.
|
|
240
248
|
this.ctx.drawImage(
|
|
@@ -251,6 +259,146 @@ export class drawutils implements DrawLib<void> {
|
|
|
251
259
|
this.ctx.restore();
|
|
252
260
|
}
|
|
253
261
|
|
|
262
|
+
/**
|
|
263
|
+
* Draw an image at the given position with the given size.<br>
|
|
264
|
+
* <br>
|
|
265
|
+
* Note: SVG images may have resizing issues at the moment.Draw a line and an arrow at the end (zB) of the given line with the specified (CSS-) color.
|
|
266
|
+
*
|
|
267
|
+
* @method texturedPoly
|
|
268
|
+
* @param {Image} textureImage - The image object to draw.
|
|
269
|
+
* @param {Bounds} textureSize - The texture size to use; these are the original bounds to map the polygon vertices to.
|
|
270
|
+
* @param {Polygon} polygon - The polygon to use as clip path.
|
|
271
|
+
* @param {Vertex} polygonPosition - The polygon's position (relative), measured at the bounding box's center.
|
|
272
|
+
* @param {number} rotation - The rotation to use for the polygon (and for the texture).
|
|
273
|
+
* @param {XYCoords={x:0,y:0}} rotationCenter - (optional) The rotational center; default is center of bounding box.
|
|
274
|
+
* @return {void}
|
|
275
|
+
* @instance
|
|
276
|
+
* @memberof drawutils
|
|
277
|
+
**/
|
|
278
|
+
texturedPoly(
|
|
279
|
+
textureImage: HTMLImageElement,
|
|
280
|
+
textureSize: Bounds,
|
|
281
|
+
polygon: Polygon,
|
|
282
|
+
polygonPosition: Vertex,
|
|
283
|
+
rotation: number
|
|
284
|
+
): void {
|
|
285
|
+
var basePolygonBounds = polygon.getBounds();
|
|
286
|
+
var targetCenterDifference = polygonPosition.clone().difference(basePolygonBounds.getCenter());
|
|
287
|
+
// var rotationalOffset = rotationCenter ? polygonPosition.difference(rotationCenter) : { x: 0, y: 0 };
|
|
288
|
+
// var rotationalOffset = { x: 0, y: 0 };
|
|
289
|
+
var tileCenter = basePolygonBounds.getCenter().sub(targetCenterDifference);
|
|
290
|
+
|
|
291
|
+
// Get the position offset of the polygon
|
|
292
|
+
var targetTextureSize = new Vertex(textureSize.width, textureSize.height);
|
|
293
|
+
// var targetTextureOffset = new Vertex(-textureSize.width / 2, -textureSize.height / 2).sub(targetCenterDifference);
|
|
294
|
+
var targetTextureOffset = new Vertex(textureSize.min.x, textureSize.min.y).sub(polygonPosition);
|
|
295
|
+
|
|
296
|
+
this.ctx.save();
|
|
297
|
+
// this.ctx.translate(this.offset.x + rotationCenter.x * this.scale.x, this.offset.y + rotationCenter.y * this.scale.y);
|
|
298
|
+
this.ctx.translate(this.offset.x + polygonPosition.x * this.scale.x, this.offset.y + polygonPosition.y * this.scale.y);
|
|
299
|
+
|
|
300
|
+
drawutils.helpers.clipPoly(
|
|
301
|
+
this.ctx,
|
|
302
|
+
{
|
|
303
|
+
x: -polygonPosition.x * this.scale.x,
|
|
304
|
+
y: -polygonPosition.y * this.scale.y
|
|
305
|
+
},
|
|
306
|
+
this.scale,
|
|
307
|
+
polygon.vertices
|
|
308
|
+
);
|
|
309
|
+
this.ctx.scale(this.scale.x, this.scale.y);
|
|
310
|
+
this.ctx.rotate(rotation);
|
|
311
|
+
this.ctx.drawImage(
|
|
312
|
+
textureImage,
|
|
313
|
+
0,
|
|
314
|
+
0,
|
|
315
|
+
textureImage.naturalWidth - 1, // There is this horrible Safari bug (fixed in newer versions)
|
|
316
|
+
textureImage.naturalHeight - 1, // To avoid errors substract 1 here.
|
|
317
|
+
targetTextureOffset.x, // * this.scale.x,
|
|
318
|
+
targetTextureOffset.y, // * this.scale.y,
|
|
319
|
+
targetTextureSize.x, // * this.scale.x,
|
|
320
|
+
targetTextureSize.y // * this.scale.y
|
|
321
|
+
);
|
|
322
|
+
|
|
323
|
+
this.ctx.restore();
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
_texturedPoly(
|
|
327
|
+
textureImage: HTMLImageElement,
|
|
328
|
+
textureSize: Bounds,
|
|
329
|
+
polygon: Polygon,
|
|
330
|
+
polygonPosition: Vertex,
|
|
331
|
+
rotation: number,
|
|
332
|
+
rotationCenter: XYCoords = { x: 0, y: 0 }
|
|
333
|
+
): void {
|
|
334
|
+
var basePolygonBounds = polygon.getBounds();
|
|
335
|
+
var targetCenterDifference = polygonPosition.clone().difference(basePolygonBounds.getCenter());
|
|
336
|
+
var rotationalOffset = rotationCenter ? polygonPosition.difference(rotationCenter) : { x: 0, y: 0 };
|
|
337
|
+
// var rotationalOffset = { x: 0, y: 0 };
|
|
338
|
+
var tileCenter = basePolygonBounds.getCenter().sub(targetCenterDifference);
|
|
339
|
+
|
|
340
|
+
// Get the position offset of the polygon
|
|
341
|
+
var targetTextureSize = new Vertex(textureSize.width, textureSize.height);
|
|
342
|
+
var targetTextureOffset = new Vertex(-textureSize.width / 2, -textureSize.height / 2).sub(targetCenterDifference);
|
|
343
|
+
|
|
344
|
+
this.ctx.save();
|
|
345
|
+
|
|
346
|
+
// this.ctx.translate(
|
|
347
|
+
// this.offset.x + (tileCenter.x - rotationalOffset.x * 0 + targetTextureOffset.x * 0.0) * this.scale.x,
|
|
348
|
+
// this.offset.y + (tileCenter.y - rotationalOffset.y * 0 + targetTextureOffset.y * 0.0) * this.scale.y
|
|
349
|
+
// );
|
|
350
|
+
this.ctx.translate(
|
|
351
|
+
this.offset.x + (tileCenter.x - rotationalOffset.x * 0 + targetTextureOffset.x * 0.0) * this.scale.x,
|
|
352
|
+
this.offset.y + (tileCenter.y - rotationalOffset.y * 0 + targetTextureOffset.y * 0.0) * this.scale.y
|
|
353
|
+
);
|
|
354
|
+
this.ctx.rotate(rotation);
|
|
355
|
+
|
|
356
|
+
drawutils.helpers.clipPoly(
|
|
357
|
+
this.ctx,
|
|
358
|
+
{
|
|
359
|
+
x: (-targetCenterDifference.x * 1 - tileCenter.x - rotationalOffset.x) * this.scale.x,
|
|
360
|
+
y: (-targetCenterDifference.y * 1 - tileCenter.y - rotationalOffset.y) * this.scale.y
|
|
361
|
+
},
|
|
362
|
+
this.scale,
|
|
363
|
+
polygon.vertices
|
|
364
|
+
);
|
|
365
|
+
this.ctx.drawImage(
|
|
366
|
+
textureImage,
|
|
367
|
+
0,
|
|
368
|
+
0,
|
|
369
|
+
textureImage.naturalWidth - 1, // There is this horrible Safari bug (fixed in newer versions)
|
|
370
|
+
textureImage.naturalHeight - 1, // To avoid errors substract 1 here.
|
|
371
|
+
(-polygonPosition.x + targetTextureOffset.x * 1 - rotationalOffset.x * 1) * this.scale.x,
|
|
372
|
+
(-polygonPosition.y + targetTextureOffset.y * 1 - rotationalOffset.y * 1) * this.scale.y,
|
|
373
|
+
targetTextureSize.x * this.scale.x,
|
|
374
|
+
targetTextureSize.y * this.scale.y
|
|
375
|
+
);
|
|
376
|
+
|
|
377
|
+
// const scaledTextureSize = new Bounds(
|
|
378
|
+
// new Vertex(
|
|
379
|
+
// -polygonPosition.x + targetTextureOffset.x - rotationalOffset.x,
|
|
380
|
+
// -polygonPosition.y + targetTextureOffset.y - rotationalOffset.y
|
|
381
|
+
// ).scaleXY(this.scale, rotationCenter),
|
|
382
|
+
// new Vertex(
|
|
383
|
+
// -polygonPosition.x + targetTextureOffset.x - rotationalOffset.x + targetTextureSize.x,
|
|
384
|
+
// -polygonPosition.y + targetTextureOffset.y - rotationalOffset.y + targetTextureSize.y
|
|
385
|
+
// ).scaleXY(this.scale, rotationCenter)
|
|
386
|
+
// );
|
|
387
|
+
// this.ctx.drawImage(
|
|
388
|
+
// textureImage,
|
|
389
|
+
// 0,
|
|
390
|
+
// 0,
|
|
391
|
+
// textureImage.naturalWidth - 1, // There is this horrible Safari bug (fixed in newer versions)
|
|
392
|
+
// textureImage.naturalHeight - 1, // To avoid errors substract 1 here.
|
|
393
|
+
// scaledTextureSize.min.x,
|
|
394
|
+
// scaledTextureSize.min.y,
|
|
395
|
+
// scaledTextureSize.width,
|
|
396
|
+
// scaledTextureSize.height
|
|
397
|
+
// );
|
|
398
|
+
|
|
399
|
+
this.ctx.restore();
|
|
400
|
+
}
|
|
401
|
+
|
|
254
402
|
/**
|
|
255
403
|
* Draw a rectangle.
|
|
256
404
|
*
|
|
@@ -784,11 +932,12 @@ export class drawutils implements DrawLib<void> {
|
|
|
784
932
|
* @param {XYCoords} center - The center of the crosshair.
|
|
785
933
|
* @param {number} radius - The radius of the crosshair.
|
|
786
934
|
* @param {string} color - The CSS color to draw the crosshair with.
|
|
935
|
+
* @param {number=0.5} lineWidth - (optional, default=0.5) The line width to use.
|
|
787
936
|
* @return {void}
|
|
788
937
|
* @instance
|
|
789
938
|
* @memberof drawutils
|
|
790
939
|
*/
|
|
791
|
-
crosshair(center: XYCoords, radius: number, color: string) {
|
|
940
|
+
crosshair(center: XYCoords, radius: number, color: string, lineWidth?: number) {
|
|
792
941
|
this.ctx.save();
|
|
793
942
|
this.ctx.beginPath();
|
|
794
943
|
this.ctx.moveTo(this.offset.x + center.x * this.scale.x - radius, this.offset.y + center.y * this.scale.y);
|
|
@@ -796,7 +945,35 @@ export class drawutils implements DrawLib<void> {
|
|
|
796
945
|
this.ctx.moveTo(this.offset.x + center.x * this.scale.x, this.offset.y + center.y * this.scale.y - radius);
|
|
797
946
|
this.ctx.lineTo(this.offset.x + center.x * this.scale.x, this.offset.y + center.y * this.scale.y + radius);
|
|
798
947
|
this.ctx.strokeStyle = color;
|
|
799
|
-
this.ctx.lineWidth = 0.5;
|
|
948
|
+
this.ctx.lineWidth = lineWidth || 0.5;
|
|
949
|
+
this.ctx.stroke();
|
|
950
|
+
this.ctx.closePath();
|
|
951
|
+
this.ctx.restore();
|
|
952
|
+
}
|
|
953
|
+
|
|
954
|
+
/**
|
|
955
|
+
* Draw a cross with diagonal axes with given radius, color and lineWidth at the given position.<br>
|
|
956
|
+
* <br>
|
|
957
|
+
* Note that the x's radius will not be affected by scaling.
|
|
958
|
+
*
|
|
959
|
+
* @method crosshair
|
|
960
|
+
* @param {XYCoords} center - The center of the crosshair.
|
|
961
|
+
* @param {number} radius - The radius of the crosshair.
|
|
962
|
+
* @param {string} color - The CSS color to draw the crosshair with.
|
|
963
|
+
* @param {number=1} lineWidth - (optional, default=1.0) The line width to use.
|
|
964
|
+
* @return {void}
|
|
965
|
+
* @instance
|
|
966
|
+
* @memberof drawutils
|
|
967
|
+
*/
|
|
968
|
+
cross(center: XYCoords, radius: number, color: string, lineWidth?: number) {
|
|
969
|
+
this.ctx.save();
|
|
970
|
+
this.ctx.beginPath();
|
|
971
|
+
this.ctx.moveTo(this.offset.x + center.x * this.scale.x - radius, this.offset.y + center.y * this.scale.y - radius);
|
|
972
|
+
this.ctx.lineTo(this.offset.x + center.x * this.scale.x + radius, this.offset.y + center.y * this.scale.y + radius);
|
|
973
|
+
this.ctx.moveTo(this.offset.x + center.x * this.scale.x - radius, this.offset.y + center.y * this.scale.y + radius);
|
|
974
|
+
this.ctx.lineTo(this.offset.x + center.x * this.scale.x + radius, this.offset.y + center.y * this.scale.y - radius);
|
|
975
|
+
this.ctx.strokeStyle = color;
|
|
976
|
+
this.ctx.lineWidth = lineWidth || 1.0;
|
|
800
977
|
this.ctx.stroke();
|
|
801
978
|
this.ctx.closePath();
|
|
802
979
|
this.ctx.restore();
|
|
@@ -830,10 +1007,12 @@ export class drawutils implements DrawLib<void> {
|
|
|
830
1007
|
* @memberof drawutils
|
|
831
1008
|
*/
|
|
832
1009
|
polyline(vertices: Array<Vertex>, isOpen: boolean, color: string, lineWidth?: number) {
|
|
833
|
-
if (vertices.length <= 1)
|
|
1010
|
+
if (vertices.length <= 1) {
|
|
1011
|
+
return;
|
|
1012
|
+
}
|
|
834
1013
|
this.ctx.save();
|
|
835
1014
|
this.ctx.beginPath();
|
|
836
|
-
this.ctx.lineWidth = lineWidth || 1.0;
|
|
1015
|
+
this.ctx.lineWidth = lineWidth * this.scale.x || 1.0;
|
|
837
1016
|
this.ctx.moveTo(this.offset.x + vertices[0].x * this.scale.x, this.offset.y + vertices[0].y * this.scale.y);
|
|
838
1017
|
for (var i = 0; i < vertices.length; i++) {
|
|
839
1018
|
this.ctx.lineTo(this.offset.x + vertices[i].x * this.scale.x, this.offset.y + vertices[i].y * this.scale.y);
|
|
@@ -992,4 +1171,20 @@ export class drawutils implements DrawLib<void> {
|
|
|
992
1171
|
this.ctx.fillStyle = color;
|
|
993
1172
|
this.ctx.fillRect(0, 0, this.ctx.canvas.width, this.ctx.canvas.height);
|
|
994
1173
|
}
|
|
1174
|
+
|
|
1175
|
+
private static helpers = {
|
|
1176
|
+
// A helper function to define the clipping path.
|
|
1177
|
+
// This could be a candidate for the draw library.
|
|
1178
|
+
clipPoly: (ctx: CanvasRenderingContext2D, offset: XYCoords, scale: XYCoords, vertices: Array<XYCoords>): void => {
|
|
1179
|
+
ctx.beginPath();
|
|
1180
|
+
// Set clip mask
|
|
1181
|
+
ctx.moveTo(offset.x + vertices[0].x * scale.x, offset.y + vertices[0].y * scale.y);
|
|
1182
|
+
for (var i = 1; i < vertices.length; i++) {
|
|
1183
|
+
const vert: XYCoords = vertices[i];
|
|
1184
|
+
ctx.lineTo(offset.x + vert.x * scale.x, offset.y + vert.y * scale.y);
|
|
1185
|
+
}
|
|
1186
|
+
ctx.closePath();
|
|
1187
|
+
ctx.clip();
|
|
1188
|
+
}
|
|
1189
|
+
};
|
|
995
1190
|
}
|
package/src/ts/drawgl.ts
CHANGED
|
@@ -6,7 +6,11 @@
|
|
|
6
6
|
* @modified 2020-10-15 Re-added the text() function.
|
|
7
7
|
* @modified 2021-01-24 Added the `setCurrentId` function.
|
|
8
8
|
* @modified 2021-05-31 Added the `setConfiguration` function from `DrawLib`.
|
|
9
|
-
* @
|
|
9
|
+
* @modified 2022-02-03 Added the `lineWidth` param to the `crosshair` function.
|
|
10
|
+
* @modified 2022-02-03 Added the `cross(...)` function.
|
|
11
|
+
* @modified 2022-03-27 Added the `texturedPoly` function.
|
|
12
|
+
* @modified 2022-07-26 Adding `alpha` to the `image(...)` function.
|
|
13
|
+
* @version 0.0.8
|
|
10
14
|
**/
|
|
11
15
|
|
|
12
16
|
import { CubicBezierCurve } from "./CubicBezierCurve";
|
|
@@ -22,6 +26,7 @@ import {
|
|
|
22
26
|
FontStyle,
|
|
23
27
|
FontWeight
|
|
24
28
|
} from "./interfaces";
|
|
29
|
+
import { Bounds } from "./Bounds";
|
|
25
30
|
|
|
26
31
|
/**
|
|
27
32
|
* @classdesc A wrapper class for basic drawing operations. This is the WebGL
|
|
@@ -275,11 +280,31 @@ export class drawutilsgl implements DrawLib<void> {
|
|
|
275
280
|
* @param {Image} image - The image object to draw.
|
|
276
281
|
* @param {Vertex} position - The position to draw the the upper left corner at.
|
|
277
282
|
* @param {Vertex} size - The x/y-size to draw the image with.
|
|
283
|
+
* @param {number=0.0} alpha - (optional, default=0.0) The transparency (0.0=opaque, 1.0=transparent).
|
|
278
284
|
* @return {void}
|
|
279
285
|
* @instance
|
|
280
286
|
* @memberof drawutils
|
|
281
287
|
**/
|
|
282
|
-
image(image: HTMLImageElement, position: Vertex, size: Vertex) {
|
|
288
|
+
image(image: HTMLImageElement, position: Vertex, size: Vertex, alpha: number = 0.0) {
|
|
289
|
+
// NOT YET IMPLEMENTED
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* Draw an image at the given position with the given size.<br>
|
|
294
|
+
* <br>
|
|
295
|
+
* Note: SVG images may have resizing issues at the moment.Draw a line and an arrow at the end (zB) of the given line with the specified (CSS-) color.
|
|
296
|
+
*
|
|
297
|
+
* @method texturedPoly
|
|
298
|
+
* @param {Image} textureImage - The image object to draw.
|
|
299
|
+
* @param {Bounds} textureSize - The texture size to use; these are the original bounds to map the polygon vertices to.
|
|
300
|
+
* @param {Polygon} polygon - The polygon to use as clip path.
|
|
301
|
+
* @param {Vertex} polygonPosition - The polygon's position (relative), measured at the bounding box's center.
|
|
302
|
+
* @param {number} rotation - The rotation to use for the polygon (and for the texture).
|
|
303
|
+
* @return {void}
|
|
304
|
+
* @instance
|
|
305
|
+
* @memberof drawutilsgl
|
|
306
|
+
**/
|
|
307
|
+
texturedPoly(textureImage: HTMLImageElement, textureSize: Bounds, polygon: Polygon, polygonPosition: Vertex, rotation: number) {
|
|
283
308
|
// NOT YET IMPLEMENTED
|
|
284
309
|
}
|
|
285
310
|
|
|
@@ -588,11 +613,30 @@ export class drawutilsgl implements DrawLib<void> {
|
|
|
588
613
|
* @param {XYCoords} center - The center of the crosshair.
|
|
589
614
|
* @param {number} radius - The radius of the crosshair.
|
|
590
615
|
* @param {string} color - The CSS color to draw the crosshair with.
|
|
616
|
+
* @param {number=0.5} lineWidth - (optional, default=0.5) The line width to use.
|
|
617
|
+
* @return {void}
|
|
618
|
+
* @instance
|
|
619
|
+
* @memberof drawutils
|
|
620
|
+
*/
|
|
621
|
+
crosshair(center: XYCoords, radius: number, color: string, lineWidth?: number) {
|
|
622
|
+
// NOT YET IMPLEMENTED
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
/**
|
|
626
|
+
* Draw a cross with diagonal axes with given radius, color and lineWidth at the given position.<br>
|
|
627
|
+
* <br>
|
|
628
|
+
* Note that the x's radius will not be affected by scaling.
|
|
629
|
+
*
|
|
630
|
+
* @method crosshair
|
|
631
|
+
* @param {XYCoords} center - The center of the crosshair.
|
|
632
|
+
* @param {number} radius - The radius of the crosshair.
|
|
633
|
+
* @param {string} color - The CSS color to draw the crosshair with.
|
|
634
|
+
* @param {number=1} lineWidth - (optional, default=1.0) The line width to use.
|
|
591
635
|
* @return {void}
|
|
592
636
|
* @instance
|
|
593
637
|
* @memberof drawutils
|
|
594
638
|
*/
|
|
595
|
-
|
|
639
|
+
cross(center: XYCoords, radius: number, color: string, lineWidth?: number) {
|
|
596
640
|
// NOT YET IMPLEMENTED
|
|
597
641
|
}
|
|
598
642
|
|
package/src/ts/drawutilssvg.ts
CHANGED
|
@@ -29,7 +29,12 @@
|
|
|
29
29
|
* @modified 2021-11-15 Adding more parameters tot the `text()` function: fontSize, textAlign, fontFamily, lineHeight.
|
|
30
30
|
* @modified 2021-11-19 Fixing the `label(text,x,y)` position.
|
|
31
31
|
* @modified 2021-11-19 Added the `color` param to the `label(...)` function.
|
|
32
|
-
* @
|
|
32
|
+
* @modified 2022-02-03 Added the `lineWidth` param to the `crosshair` function.
|
|
33
|
+
* @modified 2022-02-03 Added the `cross(...)` function.
|
|
34
|
+
* @modified 2022-03-26 Added the private `nodeDefs` and `bufferedNodeDefs` attributes.
|
|
35
|
+
* @modified 2022-03-26 Added the `texturedPoly` function to draw textures polygons.
|
|
36
|
+
* @modified 2022-07-26 Adding `alpha` to the `image(...)` function.
|
|
37
|
+
* @version 1.6.1
|
|
33
38
|
**/
|
|
34
39
|
|
|
35
40
|
import { CircleSector } from "./CircleSector";
|
|
@@ -48,6 +53,8 @@ import {
|
|
|
48
53
|
FontStyle,
|
|
49
54
|
FontWeight
|
|
50
55
|
} from "./interfaces";
|
|
56
|
+
import { Bounds } from "./Bounds";
|
|
57
|
+
import { UIDGenerator } from "./UIDGenerator";
|
|
51
58
|
|
|
52
59
|
const RAD_TO_DEG = 180 / Math.PI;
|
|
53
60
|
|
|
@@ -98,6 +105,24 @@ export class drawutilssvg implements DrawLib<void | SVGElement> {
|
|
|
98
105
|
*/
|
|
99
106
|
private nodeStyle: SVGStyleElement;
|
|
100
107
|
|
|
108
|
+
/**
|
|
109
|
+
* A style node of type `<defs>`.
|
|
110
|
+
* @member {SVGGelement}
|
|
111
|
+
* @memberof drawutilssvg
|
|
112
|
+
* @instance
|
|
113
|
+
* @private
|
|
114
|
+
*/
|
|
115
|
+
private nodeDefs: SVGDefsElement;
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* The buffered nodeDefs.
|
|
119
|
+
* @member {SVGGelement}
|
|
120
|
+
* @memberof drawutilssvg
|
|
121
|
+
* @instance
|
|
122
|
+
* @private
|
|
123
|
+
*/
|
|
124
|
+
private bufferedNodeDefs: SVGDefsElement;
|
|
125
|
+
|
|
101
126
|
/**
|
|
102
127
|
* @member {Vertex}
|
|
103
128
|
* @memberof drawutilssvg
|
|
@@ -181,7 +206,9 @@ export class drawutilssvg implements DrawLib<void | SVGElement> {
|
|
|
181
206
|
drawConfig: DrawConfig,
|
|
182
207
|
isSecondary?: boolean,
|
|
183
208
|
gNode?: SVGGElement,
|
|
184
|
-
bufferGNode?: SVGGElement
|
|
209
|
+
bufferGNode?: SVGGElement,
|
|
210
|
+
nodeDefs?: SVGDefsElement,
|
|
211
|
+
bufferNodeDefs?: SVGDefsElement
|
|
185
212
|
) {
|
|
186
213
|
this.svgNode = svgNode;
|
|
187
214
|
this.offset = new Vertex(0, 0).set(offset);
|
|
@@ -195,14 +222,23 @@ export class drawutilssvg implements DrawLib<void | SVGElement> {
|
|
|
195
222
|
if (isSecondary) {
|
|
196
223
|
this.gNode = gNode;
|
|
197
224
|
this.bufferGNode = bufferGNode;
|
|
225
|
+
this.nodeDefs = nodeDefs;
|
|
226
|
+
this.bufferedNodeDefs = bufferNodeDefs;
|
|
198
227
|
} else {
|
|
199
228
|
this.addStyleDefs(drawConfig);
|
|
229
|
+
this.addDefsNode();
|
|
200
230
|
this.gNode = this.createSVGNode("g") as SVGGElement;
|
|
201
231
|
this.bufferGNode = this.createSVGNode("g") as SVGGElement;
|
|
202
232
|
this.svgNode.appendChild(this.gNode);
|
|
203
233
|
}
|
|
204
234
|
}
|
|
205
235
|
|
|
236
|
+
/**
|
|
237
|
+
* Adds a default style defintion based on the passed DrawConfig.
|
|
238
|
+
* Twaek the draw config to change default colors or line thicknesses.
|
|
239
|
+
*
|
|
240
|
+
* @param {DrawConfig} drawConfig
|
|
241
|
+
*/
|
|
206
242
|
private addStyleDefs(drawConfig: DrawConfig) {
|
|
207
243
|
this.nodeStyle = this.createSVGNode("style") as SVGStyleElement;
|
|
208
244
|
this.svgNode.appendChild(this.nodeStyle);
|
|
@@ -236,6 +272,16 @@ export class drawutilssvg implements DrawLib<void | SVGElement> {
|
|
|
236
272
|
this.nodeStyle.innerHTML = rules.join("\n");
|
|
237
273
|
}
|
|
238
274
|
|
|
275
|
+
/**
|
|
276
|
+
* Adds the internal <defs> node.
|
|
277
|
+
*/
|
|
278
|
+
private addDefsNode() {
|
|
279
|
+
this.nodeDefs = this.createSVGNode("defs") as SVGDefsElement;
|
|
280
|
+
// this.svgNode.appendChild(this.nodeDefs);
|
|
281
|
+
this.bufferedNodeDefs = this.createSVGNode("defs") as SVGDefsElement;
|
|
282
|
+
this.svgNode.appendChild(this.nodeDefs);
|
|
283
|
+
}
|
|
284
|
+
|
|
239
285
|
/**
|
|
240
286
|
* This is a simple way to include custom CSS class mappings to the style defs of the generated SVG.
|
|
241
287
|
*
|
|
@@ -386,7 +432,9 @@ export class drawutilssvg implements DrawLib<void | SVGElement> {
|
|
|
386
432
|
null, // no DrawConfig
|
|
387
433
|
true, // isSecondary
|
|
388
434
|
this.gNode,
|
|
389
|
-
this.bufferGNode
|
|
435
|
+
this.bufferGNode,
|
|
436
|
+
this.nodeDefs,
|
|
437
|
+
this.bufferedNodeDefs
|
|
390
438
|
);
|
|
391
439
|
return copy;
|
|
392
440
|
}
|
|
@@ -470,11 +518,16 @@ export class drawutilssvg implements DrawLib<void | SVGElement> {
|
|
|
470
518
|
// layout updates.
|
|
471
519
|
// Replace the old <g>-node with the buffer node.
|
|
472
520
|
// https://stackoverflow.com/questions/27442464/how-to-update-a-svg-image-without-seeing-a-blinking
|
|
521
|
+
this.svgNode.replaceChild(this.bufferedNodeDefs, this.nodeDefs);
|
|
473
522
|
this.svgNode.replaceChild(this.bufferGNode, this.gNode);
|
|
474
523
|
}
|
|
475
|
-
|
|
524
|
+
const tmpGNode: SVGGElement = this.gNode;
|
|
476
525
|
this.gNode = this.bufferGNode;
|
|
477
|
-
this.bufferGNode =
|
|
526
|
+
this.bufferGNode = tmpGNode;
|
|
527
|
+
|
|
528
|
+
const tmpDefsNode: SVGDefsElement = this.nodeDefs;
|
|
529
|
+
this.nodeDefs = this.bufferedNodeDefs;
|
|
530
|
+
this.bufferedNodeDefs = tmpDefsNode;
|
|
478
531
|
}
|
|
479
532
|
|
|
480
533
|
private _x(x: number): number {
|
|
@@ -542,11 +595,12 @@ export class drawutilssvg implements DrawLib<void | SVGElement> {
|
|
|
542
595
|
* @param {Image} image - The image object to draw.
|
|
543
596
|
* @param {Vertex} position - The position to draw the the upper left corner at.
|
|
544
597
|
* @param {Vertex} size - The x/y-size to draw the image with.
|
|
598
|
+
* @param {number=0.0} alpha - (optional, default=0.0) The transparency (1.0=opaque, 0.0=transparent).
|
|
545
599
|
* @return {void}
|
|
546
600
|
* @instance
|
|
547
601
|
* @memberof drawutilssvg
|
|
548
602
|
**/
|
|
549
|
-
image(image: HTMLImageElement, position: Vertex, size: Vertex) {
|
|
603
|
+
image(image: HTMLImageElement, position: Vertex, size: Vertex, alpha: number = 1.0) {
|
|
550
604
|
const node: SVGElement = this.makeNode("image");
|
|
551
605
|
|
|
552
606
|
// We need to re-adjust the image if it was not yet fully loaded before.
|
|
@@ -557,6 +611,9 @@ export class drawutilssvg implements DrawLib<void | SVGElement> {
|
|
|
557
611
|
node.setAttribute("width", `${image.naturalWidth * this.scale.x}`);
|
|
558
612
|
node.setAttribute("height", `${image.naturalHeight * this.scale.y}`);
|
|
559
613
|
node.setAttribute("display", null); // Dislay when loaded
|
|
614
|
+
// if (alpha) {
|
|
615
|
+
node.setAttribute("opacity", `${alpha}`);
|
|
616
|
+
// }
|
|
560
617
|
node.setAttribute("transform", `translate(${this._x(position.x)} ${this._y(position.y)}) scale(${ratioX} ${ratioY})`);
|
|
561
618
|
}
|
|
562
619
|
};
|
|
@@ -574,6 +631,90 @@ export class drawutilssvg implements DrawLib<void | SVGElement> {
|
|
|
574
631
|
return this._bindFillDraw(node, "image", null, null);
|
|
575
632
|
}
|
|
576
633
|
|
|
634
|
+
/**
|
|
635
|
+
* Draw an image at the given position with the given size.<br>
|
|
636
|
+
* <br>
|
|
637
|
+
* Note: SVG images may have resizing issues at the moment.Draw a line and an arrow at the end (zB) of the given line with the specified (CSS-) color.
|
|
638
|
+
*
|
|
639
|
+
* @method texturedPoly
|
|
640
|
+
* @param {Image} textureImage - The image object to draw.
|
|
641
|
+
* @param {Bounds} textureSize - The texture size to use; these are the original bounds to map the polygon vertices to.
|
|
642
|
+
* @param {Polygon} polygon - The polygon to use as clip path.
|
|
643
|
+
* @param {Vertex} polygonPosition - The polygon's position (relative), measured at the bounding box's center.
|
|
644
|
+
* @param {number} rotation - The rotation to use for the polygon (and for the texture).
|
|
645
|
+
* @return {void}
|
|
646
|
+
* @instance
|
|
647
|
+
* @memberof drawutilssvg
|
|
648
|
+
**/
|
|
649
|
+
texturedPoly(
|
|
650
|
+
textureImage: HTMLImageElement,
|
|
651
|
+
textureSize: Bounds,
|
|
652
|
+
polygon: Polygon,
|
|
653
|
+
polygonPosition: Vertex,
|
|
654
|
+
rotation: number
|
|
655
|
+
): SVGElement {
|
|
656
|
+
const basePolygonBounds: Bounds = polygon.getBounds();
|
|
657
|
+
const rotatedScalingOrigin = new Vertex(textureSize.min).clone().rotate(rotation, polygonPosition);
|
|
658
|
+
const rotationCenter = polygonPosition.clone().add(rotatedScalingOrigin.difference(textureSize.min).inv());
|
|
659
|
+
// Create something like this
|
|
660
|
+
// ...
|
|
661
|
+
// <defs>
|
|
662
|
+
// <clipPath id="shape">
|
|
663
|
+
// <path fill="none" d="..."/>
|
|
664
|
+
// </clipPath>
|
|
665
|
+
// </defs>
|
|
666
|
+
// ...
|
|
667
|
+
// <g clip-path="url(#shape)">
|
|
668
|
+
// <g transform="scale(...)">
|
|
669
|
+
// <image width="643" height="643" transform="rotate(...)" xlink:href="https://s3-us-west-2.amazonaws.com/s.cdpn.io/222579/beagle400.jpg" >
|
|
670
|
+
// </g>
|
|
671
|
+
// </g>
|
|
672
|
+
// </image>
|
|
673
|
+
// ...
|
|
674
|
+
const clipPathNode: SVGClipPathElement = this.makeNode("clipPath") as SVGClipPathElement;
|
|
675
|
+
const clipPathId: string = `clippath_${UIDGenerator.next()}`; // TODO: use a better UUID generator here?
|
|
676
|
+
clipPathNode.setAttribute("id", clipPathId);
|
|
677
|
+
|
|
678
|
+
const gNode = this.makeNode("g") as SVGGElement;
|
|
679
|
+
const imageNode: SVGImageElement = this.makeNode("image") as SVGImageElement;
|
|
680
|
+
imageNode.setAttribute("x", `${this._x(rotatedScalingOrigin.x)}`);
|
|
681
|
+
imageNode.setAttribute("y", `${this._y(rotatedScalingOrigin.y)}`);
|
|
682
|
+
imageNode.setAttribute("width", `${textureSize.width}`);
|
|
683
|
+
imageNode.setAttribute("height", `${textureSize.height}`);
|
|
684
|
+
imageNode.setAttribute("href", textureImage.src);
|
|
685
|
+
// imageNode.setAttribute("opacity", "0.5");
|
|
686
|
+
// SVG rotations in degrees
|
|
687
|
+
imageNode.setAttribute(
|
|
688
|
+
"transform",
|
|
689
|
+
`rotate(${rotation * RAD_TO_DEG}, ${this._x(rotatedScalingOrigin.x)}, ${this._y(rotatedScalingOrigin.y)})`
|
|
690
|
+
);
|
|
691
|
+
const pathNode: SVGPathElement = this.makeNode("path") as SVGPathElement;
|
|
692
|
+
const pathData: string[] = [];
|
|
693
|
+
if (polygon.vertices.length > 0) {
|
|
694
|
+
const self = this;
|
|
695
|
+
pathData.push("M", `${this._x(polygon.vertices[0].x)}`, `${this._y(polygon.vertices[0].y)}`);
|
|
696
|
+
for (var i = 1; i < polygon.vertices.length; i++) {
|
|
697
|
+
pathData.push("L", `${this._x(polygon.vertices[i].x)}`, `${this._y(polygon.vertices[i].y)}`);
|
|
698
|
+
}
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
pathNode.setAttribute("d", pathData.join(" "));
|
|
702
|
+
clipPathNode.appendChild(pathNode);
|
|
703
|
+
this.bufferedNodeDefs.appendChild(clipPathNode);
|
|
704
|
+
|
|
705
|
+
gNode.appendChild(imageNode);
|
|
706
|
+
gNode.setAttribute("transform-origin", `${this._x(rotatedScalingOrigin.x)} ${this._y(rotatedScalingOrigin.y)}`);
|
|
707
|
+
gNode.setAttribute("transform", `scale(${this.scale.x}, ${this.scale.y})`);
|
|
708
|
+
|
|
709
|
+
const clipNode: SVGGElement = this.makeNode("g") as SVGGElement;
|
|
710
|
+
clipNode.appendChild(gNode);
|
|
711
|
+
clipNode.setAttribute("clip-path", `url(#${clipPathId})`);
|
|
712
|
+
|
|
713
|
+
// TODO: check if the image class is correct here or if we should use a 'clippedImage' class here
|
|
714
|
+
this._bindFillDraw(clipNode, "image", null, null); // No color, no lineWidth
|
|
715
|
+
return clipNode;
|
|
716
|
+
}
|
|
717
|
+
|
|
577
718
|
/**
|
|
578
719
|
* Draw the given (cubic) bézier curve.
|
|
579
720
|
*
|
|
@@ -1020,11 +1161,12 @@ export class drawutilssvg implements DrawLib<void | SVGElement> {
|
|
|
1020
1161
|
* @param {XYCoords} center - The center of the crosshair.
|
|
1021
1162
|
* @param {number} radius - The radius of the crosshair.
|
|
1022
1163
|
* @param {string} color - The CSS color to draw the crosshair with.
|
|
1164
|
+
* @param {number=0.5} lineWidth - (optional, default=0.5) The line width to use.
|
|
1023
1165
|
* @return {void}
|
|
1024
1166
|
* @instance
|
|
1025
1167
|
* @memberof drawutilssvg
|
|
1026
1168
|
*/
|
|
1027
|
-
crosshair(center: XYCoords, radius: number, color: string) {
|
|
1169
|
+
crosshair(center: XYCoords, radius: number, color: string, lineWidth?: number) {
|
|
1028
1170
|
const node: SVGElement = this.makeNode("path");
|
|
1029
1171
|
const d: SVGPathParams = [
|
|
1030
1172
|
"M",
|
|
@@ -1041,7 +1183,41 @@ export class drawutilssvg implements DrawLib<void | SVGElement> {
|
|
|
1041
1183
|
this._y(center.y) + radius
|
|
1042
1184
|
];
|
|
1043
1185
|
node.setAttribute("d", d.join(" "));
|
|
1044
|
-
return this._bindFillDraw(node, "crosshair", color, 0.5);
|
|
1186
|
+
return this._bindFillDraw(node, "crosshair", color, lineWidth || 0.5);
|
|
1187
|
+
}
|
|
1188
|
+
|
|
1189
|
+
/**
|
|
1190
|
+
* Draw a cross with diagonal axes with given radius, color and lineWidth at the given position.<br>
|
|
1191
|
+
* <br>
|
|
1192
|
+
* Note that the x's radius will not be affected by scaling.
|
|
1193
|
+
*
|
|
1194
|
+
* @method crosshair
|
|
1195
|
+
* @param {XYCoords} center - The center of the crosshair.
|
|
1196
|
+
* @param {number} radius - The radius of the crosshair.
|
|
1197
|
+
* @param {string} color - The CSS color to draw the crosshair with.
|
|
1198
|
+
* @param {number=1} lineWidth - (optional, default=1.0) The line width to use.
|
|
1199
|
+
* @return {void}
|
|
1200
|
+
* @instance
|
|
1201
|
+
* @memberof drawutils
|
|
1202
|
+
*/
|
|
1203
|
+
cross(center: XYCoords, radius: number, color: string, lineWidth?: number) {
|
|
1204
|
+
const node: SVGElement = this.makeNode("path");
|
|
1205
|
+
const d: SVGPathParams = [
|
|
1206
|
+
"M",
|
|
1207
|
+
this._x(center.x) - radius,
|
|
1208
|
+
this._y(center.y) - radius,
|
|
1209
|
+
"L",
|
|
1210
|
+
this._x(center.x) + radius,
|
|
1211
|
+
this._y(center.y) + radius,
|
|
1212
|
+
"M",
|
|
1213
|
+
this._x(center.x) - radius,
|
|
1214
|
+
this._y(center.y) + radius,
|
|
1215
|
+
"L",
|
|
1216
|
+
this._x(center.x) + radius,
|
|
1217
|
+
this._y(center.y) - radius
|
|
1218
|
+
];
|
|
1219
|
+
node.setAttribute("d", d.join(" "));
|
|
1220
|
+
return this._bindFillDraw(node, "cross", color, lineWidth || 1.0);
|
|
1045
1221
|
}
|
|
1046
1222
|
|
|
1047
1223
|
/**
|
|
@@ -1226,14 +1402,6 @@ export class drawutilssvg implements DrawLib<void | SVGElement> {
|
|
|
1226
1402
|
if (this.isSecondary) {
|
|
1227
1403
|
return;
|
|
1228
1404
|
}
|
|
1229
|
-
// // Clearing an SVG is equivalent to removing all its child elements.
|
|
1230
|
-
// for (var i = 0; i < this.gNode.childNodes.length; i++) {
|
|
1231
|
-
// // Hide all nodes here. Don't throw them away.
|
|
1232
|
-
// // We can probably re-use them in the next draw cycle.
|
|
1233
|
-
// var child: SVGElement = this.gNode.childNodes[i] as SVGElement;
|
|
1234
|
-
// this.cache.set(child.getAttribute("id"), child);
|
|
1235
|
-
// }
|
|
1236
|
-
// this.removeAllChildNodes();
|
|
1237
1405
|
|
|
1238
1406
|
// Add a covering rect with the given background color
|
|
1239
1407
|
this.curId = "background";
|
|
@@ -1262,6 +1430,9 @@ export class drawutilssvg implements DrawLib<void | SVGElement> {
|
|
|
1262
1430
|
while (this.bufferGNode.lastChild) {
|
|
1263
1431
|
this.bufferGNode.removeChild(this.bufferGNode.lastChild);
|
|
1264
1432
|
}
|
|
1433
|
+
while (this.bufferedNodeDefs.lastChild) {
|
|
1434
|
+
this.bufferedNodeDefs.removeChild(this.bufferedNodeDefs.lastChild);
|
|
1435
|
+
}
|
|
1265
1436
|
}
|
|
1266
1437
|
|
|
1267
1438
|
/**
|
package/src/ts/index.ts
CHANGED