Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
nswamy14 committed Nov 29, 2022
1 parent ba89204 commit 1a741c4
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 148 deletions.
97 changes: 34 additions & 63 deletions dist/node-i2d.js
Original file line number Diff line number Diff line change
Expand Up @@ -2352,17 +2352,12 @@ function exec(value) {
return this;
}

function on(eventType, hndlr) {
for (var i = 0, len = this.stack.length; i < len; i += 1) {
this.stack[i].on(eventType, hndlr);
}
// function on(eventType, hndlr) {
// for (let i = 0, len = this.stack.length; i < len; i += 1) {
// this.stack[i].on(eventType, hndlr);
// }

return this;
} // function in (coOr) {
// for (let i = 0, len = this.stack.length; i < len; i += 1) {
// this.stack[i].in(coOr)
// }
// return this
// return this;
// }

function remove() {
Expand All @@ -2373,13 +2368,30 @@ function remove() {
return this;
}

function interrupt() {
for (var i = 0, len = this.stack.length; i < len; i += 1) {
this.stack[i].interrupt();
}
// function interrupt() {
// for (let i = 0, len = this.stack.length; i < len; i += 1) {
// this.stack[i].interrupt();
// }

return this;
}
// return this;
// }

// function resolveObject(config, node, i) {
// const obj = {};
// let key;

// for (key in config) {
// if (key !== "end") {
// if (typeof config[key] === "function") {
// obj[key] = config[key].call(node, node.dataObj, i);
// } else {
// obj[key] = config[key];
// }
// }
// }

// return obj;
// }

var textArray = function textArray(value) {
var node;
Expand Down Expand Up @@ -2476,14 +2488,9 @@ CollectionPrototype.prototype = {
rotate: rotate,
scale: scale,
exec: exec,
// animateTo: animateArrayTo,
// animateExe: animateArrayExe,
// animatePathTo: animatePathArrayTo,
remove: remove,
interrupt: interrupt,
text: textArray,
join: join,
on: on,
};

CollectionPrototype.prototype.createNode = function () {};
Expand Down Expand Up @@ -2739,8 +2746,6 @@ function PixelObject(data, width, height) {
this.imageData = data;
this.width = width;
this.height = height;
// this.x = x;
// this.y = y;
}

PixelObject.prototype.get = function (pos) {
Expand Down Expand Up @@ -2896,7 +2901,6 @@ function CanvasDom() {

CanvasDom.prototype = {
render: cRender,
// on: addListener,
setAttr: domSetAttribute,
setStyle: domSetStyle,
applyStyles: applyStyles,
Expand All @@ -2906,15 +2910,12 @@ function imageInstance(self) {
var imageIns = new Image();
imageIns.crossOrigin = "anonymous";
imageIns.dataMode = Image.MODE_MIME | Image.MODE_IMAGE;
// console.log(self.ctx.type_);
imageIns.onload = function onload() {
self.attr.height = self.attr.height ? self.attr.height : imageIns.naturalHeight;
self.attr.width = self.attr.width ? self.attr.width : imageIns.naturalWidth;
self.imageObj = imageIns;

if (self.nodeExe.attr.onload && typeof self.nodeExe.attr.onload === "function") {
// console.log(self);
// console.log(self.nodeExe);
self.nodeExe.attr.onload.call(self.nodeExe, self.image);
}

Expand Down Expand Up @@ -3058,7 +3059,9 @@ RenderText.prototype.fitWidth = function () {
if (this.ctx.measureText(strLit + textList[i]).width < width) {
strLit = strLit + textList[i];
} else {
textSubStrs.push(strLit);
if (strLit && strLit.length > 0) {
textSubStrs.push(strLit);
}
strLit = textList[i];
}
}
Expand Down Expand Up @@ -3415,14 +3418,6 @@ RenderPath.prototype.updateBBox = function RPupdateBBox() {
var scaleX = ref$1.scaleX;
var scaleY = ref$1.scaleY;

// if (transform && transform.translate) {
// [translateX, translateY] = transform.translate;
// }

// if (transform && transform.scale) {
// [scaleX = 1, scaleY = scaleX] = transform.scale;
// }

self.BBox = self.path
? self.path.BBox
: {
Expand Down Expand Up @@ -3623,10 +3618,6 @@ RenderEllipse.prototype.constructor = RenderEllipse;

RenderEllipse.prototype.updateBBox = function REupdateBBox() {
var self = this;
// let translateX = 0;
// let translateY = 0;
// let scaleX = 1;
// let scaleY = 1;
var ref = self.attr;
var transform = ref.transform;
var cx = ref.cx; if ( cx === void 0 ) cx = 0;
Expand All @@ -3639,14 +3630,6 @@ RenderEllipse.prototype.updateBBox = function REupdateBBox() {
var scaleX = ref$1.scaleX;
var scaleY = ref$1.scaleY;

// if (transform && transform.translate) {
// [translateX, translateY] = transform.translate;
// }

// if (transform && transform.scale) {
// [scaleX = 1, scaleY = scaleX] = transform.scale;
// }

self.BBox = {
x: translateX + (cx - rx) * scaleX,
y: translateY + (cy - ry) * scaleY,
Expand Down Expand Up @@ -4002,13 +3985,6 @@ var CanvasNodeExe = function CanvasNodeExe(context, config, id, vDomIndex) {

this.dom.nodeExe = this;
this.BBoxUpdate = true;
// if (config.style) {
// this.setStyle(config.style);
// }

// if (config.attr) {
// this.setAttr(config.attr);
// }
};

CanvasNodeExe.prototype = new NodePrototype();
Expand Down Expand Up @@ -4202,7 +4178,7 @@ CanvasNodeExe.prototype.execute = function Cexecute(disableRestore) {
if (this.style.display === "none") {
return;
}
console.log(disableRestore);
// console.log(disableRestore);
if (!disableRestore) {
this.ctx.save();
}
Expand Down Expand Up @@ -4493,8 +4469,6 @@ function RenderTexture(nodeExe, config) {
if (key !== "height" && key !== "width")
{ self.setAttr(key, self.attr[key]); }
}

// self.stack = [self];
}
RenderTexture.prototype = new NodePrototype();
RenderTexture.prototype.constructor = RenderTexture;
Expand Down Expand Up @@ -4619,9 +4593,8 @@ function createPage (ctx) {
);
root.ENV = "NODE";
var execute = root.execute.bind(root);
var ratio = 1;
var onClear = function (ctx, width, height) {
ctx.clearRect(0, 0, width * ratio, height * ratio);
ctx.clearRect(0, 0, width, height);
};

root.setClear = function (exe) {
Expand Down Expand Up @@ -4658,8 +4631,6 @@ function createPage (ctx) {
};

root.setSize = function (width_, height_) {
// width = width_;
// height = height_;
this.domEl = createCanvas(width_, height_, "pdf");
ctx = this.domEl.getContext("2d", config);
ctx.type_ = "pdf";
Expand All @@ -4671,7 +4642,7 @@ function createPage (ctx) {

root.execute = function executeExe() {
onClear(ctx, this.width, this.height);
ctx.setTransform(ratio, 0, 0, ratio, 0, 0);
ctx.setTransform(1, 0, 0, 1, 0, 0);
this.updateBBox();
execute();
if (onChangeExe && this.stateModified) {
Expand Down
5 changes: 1 addition & 4 deletions examples/Example5.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@
sheight: 140,
},
onload: function (argument) {
console.log("onload called");
// console.log("onload called");
}
},
});
Expand All @@ -181,9 +181,6 @@
},
});




var clipInstance = canvasInstance.createClip();
clipInstance.clip.createEl({
el: "polygon",
Expand Down
Binary file modified examples/examplej.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
50 changes: 6 additions & 44 deletions src/modules/canvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,6 @@ function PixelObject(data, width, height) {
this.imageData = data;
this.width = width;
this.height = height;
// this.x = x;
// this.y = y;
}

PixelObject.prototype.get = function (pos) {
Expand Down Expand Up @@ -378,7 +376,6 @@ function CanvasDom() {

CanvasDom.prototype = {
render: cRender,
// on: addListener,
setAttr: domSetAttribute,
setStyle: domSetStyle,
applyStyles,
Expand All @@ -388,15 +385,12 @@ function imageInstance(self) {
const imageIns = new Image();
imageIns.crossOrigin = "anonymous";
imageIns.dataMode = Image.MODE_MIME | Image.MODE_IMAGE
// console.log(self.ctx.type_);
imageIns.onload = function onload() {
self.attr.height = self.attr.height ? self.attr.height : imageIns.naturalHeight;
self.attr.width = self.attr.width ? self.attr.width : imageIns.naturalWidth;
self.imageObj = imageIns;

if (self.nodeExe.attr.onload && typeof self.nodeExe.attr.onload === "function") {
// console.log(self);
// console.log(self.nodeExe);
self.nodeExe.attr.onload.call(self.nodeExe, self.image);
}

Expand Down Expand Up @@ -523,7 +517,9 @@ RenderText.prototype.fitWidth = function () {
if (this.ctx.measureText(strLit + textList[i]).width < width) {
strLit = strLit + textList[i];
} else {
textSubStrs.push(strLit);
if (strLit && strLit.length > 0) {
textSubStrs.push(strLit);
}
strLit = textList[i];
}
}
Expand Down Expand Up @@ -832,14 +828,6 @@ RenderPath.prototype.updateBBox = function RPupdateBBox() {
const { transform } = self.attr;
const { translateX, translateY, scaleX, scaleY } = parseTransform(transform);

// if (transform && transform.translate) {
// [translateX, translateY] = transform.translate;
// }

// if (transform && transform.scale) {
// [scaleX = 1, scaleY = scaleX] = transform.scale;
// }

self.BBox = self.path
? self.path.BBox
: {
Expand Down Expand Up @@ -1040,21 +1028,9 @@ RenderEllipse.prototype.constructor = RenderEllipse;

RenderEllipse.prototype.updateBBox = function REupdateBBox() {
const self = this;
// let translateX = 0;
// let translateY = 0;
// let scaleX = 1;
// let scaleY = 1;
const { transform, cx = 0, cy = 0, rx = 0, ry = 0 } = self.attr;
const { translateX, translateY, scaleX, scaleY } = parseTransform(transform);

// if (transform && transform.translate) {
// [translateX, translateY] = transform.translate;
// }

// if (transform && transform.scale) {
// [scaleX = 1, scaleY = scaleX] = transform.scale;
// }

self.BBox = {
x: translateX + (cx - rx) * scaleX,
y: translateY + (cy - ry) * scaleY,
Expand Down Expand Up @@ -1367,13 +1343,6 @@ const CanvasNodeExe = function CanvasNodeExe(context, config, id, vDomIndex) {

this.dom.nodeExe = this;
this.BBoxUpdate = true;
// if (config.style) {
// this.setStyle(config.style);
// }

// if (config.attr) {
// this.setAttr(config.attr);
// }
};

CanvasNodeExe.prototype = new NodePrototype();
Expand Down Expand Up @@ -1566,7 +1535,7 @@ CanvasNodeExe.prototype.execute = function Cexecute(disableRestore) {
if (this.style.display === "none") {
return;
}
console.log(disableRestore);
// console.log(disableRestore);
if (!disableRestore) {
this.ctx.save();
}
Expand Down Expand Up @@ -1847,8 +1816,6 @@ function RenderTexture(nodeExe, config = {}) {
if (key !== "height" && key !== "width")
self.setAttr(key, self.attr[key]);
}

// self.stack = [self];
}
RenderTexture.prototype = new NodePrototype();
RenderTexture.prototype.constructor = RenderTexture;
Expand Down Expand Up @@ -1971,9 +1938,8 @@ function createPage (ctx) {
);
root.ENV = "NODE";
const execute = root.execute.bind(root);
const ratio = 1;
let onClear = function (ctx, width, height) {
ctx.clearRect(0, 0, width * ratio, height * ratio);
ctx.clearRect(0, 0, width, height);
};

root.setClear = function (exe) {
Expand Down Expand Up @@ -2010,8 +1976,6 @@ function createPage (ctx) {
};

root.setSize = function (width_, height_) {
// width = width_;
// height = height_;
this.domEl = createCanvas(width_, height_, "pdf");
ctx = this.domEl.getContext("2d", config);
ctx.type_ = "pdf";
Expand All @@ -2023,7 +1987,7 @@ function createPage (ctx) {

root.execute = function executeExe() {
onClear(ctx, this.width, this.height);
ctx.setTransform(ratio, 0, 0, ratio, 0, 0);
ctx.setTransform(1, 0, 0, 1, 0, 0);
this.updateBBox();
execute();
if (onChangeExe && this.stateModified) {
Expand Down Expand Up @@ -2089,7 +2053,6 @@ function createPage (ctx) {
function pdfLayer(config, height = 0, width = 0) {
const layer = createCanvas(width, height, "pdf");
let ctx = layer.getContext("2d", config);
const ratio = 1;
ctx.type_ = "pdf";
ctx.quality = "best";
ctx.patternQuality = "best";
Expand Down Expand Up @@ -2161,7 +2124,6 @@ function canvasLayer(config, height = 0, width = 0) {
let onChangeExe;
const layer = createCanvas(width, height);
let ctx = layer.getContext("2d", config);
const ratio = 1;
let root = createPage(ctx);
root.domEl = layer;
root.height = height;
Expand Down
Loading

0 comments on commit 1a741c4

Please sign in to comment.