Browse Source

Path object

tentone 5 years ago
parent
commit
ce49ebf229
3 changed files with 92 additions and 0 deletions
  1. 2 0
      examples/charts.html
  2. 1 0
      source/Escher.js
  3. 89 0
      source/objects/Path.js

+ 2 - 0
examples/charts.html

@@ -82,6 +82,8 @@
 		pie.draggable = true;
 		pie.draggable = true;
 		group.add(pie);
 		group.add(pie);
 
 
+		var path = new Path2D("M10 10 h 80 v 80 h -80 Z");
+
 		var viewport = new Escher.Viewport(canvas);
 		var viewport = new Escher.Viewport(canvas);
 
 
 		var renderer = new Escher.Renderer(canvas);
 		var renderer = new Escher.Renderer(canvas);

+ 1 - 0
source/Escher.js

@@ -29,6 +29,7 @@ export {ScatterGraph} from "./objects/chart/ScatterGraph.js";
 export {BarGraph} from "./objects/chart/BarGraph.js";
 export {BarGraph} from "./objects/chart/BarGraph.js";
 export {Gauge} from "./objects/chart/Gauge.js";
 export {Gauge} from "./objects/chart/Gauge.js";
 export {PieChart} from "./objects/chart/PieChart.js";
 export {PieChart} from "./objects/chart/PieChart.js";
+export {Path} from "./objects/Path.js";
 
 
 export {Style} from "./objects/style/Style.js";
 export {Style} from "./objects/style/Style.js";
 export {ColorStyle} from "./objects/style/ColorStyle.js";
 export {ColorStyle} from "./objects/style/ColorStyle.js";

+ 89 - 0
source/objects/Path.js

@@ -0,0 +1,89 @@
+import {Object2D} from "../Object2D.js";
+import {Vector2} from "../math/Vector2.js";
+import {Box2} from "../math/Box2.js";
+import {ColorStyle} from "./style/ColorStyle";
+import {Style} from "./style/Style";
+
+/**
+ * Path object can be used to draw paths build from commands into the canvas.
+ * 
+ * These paths can be also obtained from SVG files as a SVG command list.
+ *
+ * @class
+ * @extends {Object2D}
+ */
+function Path(path)
+{
+	Object2D.call(this);
+
+	/**
+	 * Path2D object containing the commands to draw the shape into the canvas.
+	 * 
+	 * Check https://developer.mozilla.org/en-US/docs/Web/API/Path2D/Path2D for more details. 
+	 */
+	this.path = path !== undefined ? path : new Path2D("M10 10 h 80 v 80 h -80 Z");
+
+	/**
+	 * Style of the object border line.
+	 *
+	 * If set null it is ignored.
+	 */
+	this.strokeStyle = new ColorStyle("#000000");
+
+	/**
+	 * Line width, only used if a valid strokeStyle is defined.
+	 */
+	this.lineWidth = 1;
+
+	/**
+	 * Background color of the path.
+	 *
+	 * If set null it is ignored.
+	 *
+	 * @param {Style}
+	 */
+	this.fillStyle = new ColorStyle("#FFFFFF");
+}
+
+Path.prototype = Object.create(Object2D.prototype);
+Path.prototype.constructor = Path;
+Path.prototype.type = "Path";
+Object2D.register(Path, "Path");
+
+Path.prototype.draw = function(context, viewport, canvas)
+{
+	if(this.fillStyle !== null)
+	{	
+		context.fillStyle = this.fillStyle.get(context);
+		context.fill(this.path);
+	}
+
+	if(this.strokeStyle !== null)
+	{
+		context.lineWidth = this.lineWidth;
+		context.strokeStyle = this.strokeStyle.get(context);
+		context.stroke(this.path);
+	}
+};
+
+Path.prototype.serialize = function(recursive)
+{
+	var data = Object2D.prototype.serialize.call(this, recursive);
+
+	data.strokeStyle = this.strokeStyle !== null ? this.strokeStyle.serialize() : null;
+	data.lineWidth = this.lineWidth;
+	data.fillStyle = this.fillStyle !== null ? this.fillStyle.serialize() : null;
+
+	return data;
+};
+
+Path.prototype.parse = function(data, root)
+{
+	Object2D.prototype.parse.call(this, data, root);
+
+	this.strokeStyle = data.strokeStyle !== null ? Style.parse(data.strokeStyle) : null;
+	this.lineWidth = data.lineWidth;
+	this.fillStyle = data.fillStyle !== null ? Style.parse(data.fillStyle) : null;
+};
+
+export {Path};