-
Notifications
You must be signed in to change notification settings - Fork 57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Creating Polygon based on Template #9
Comments
I had used the Invent command for creating the Triangle as follows SVG.Triangle = SVG.invent({
// Initialize node
create: 'polygon'
// Inherit from
, inherit: SVG.Shape
// Add parent method
, construct: {
// Create a wrapped polygon element
triangle: function(p) {
return this.put(new SVG.Triangle).plot(p)
}
}
}) and while creating, i am creating this triangle as follows var triDraw = SVG.get('cvs');
var triangleEle = triDraw.triangle('100,100 150,100 125,150').fill('none').stroke({ width: 1 }) |
You are almost there. I now can see, that I did a design mistake in this plugin by using the type of the node as key. But however: You need to add a custom constructor function. In this function you have to set SVG.Element.prototype.draw.extend('rect image triangle', { I will soon add a feature, to add a type to an already existing draw-function so you can add your triangle to the I did not explained it very well. Please ask when I should clarify a things! |
Thanks a lot Ulrich, will try to check the code and implement as you are telling and will come back to you. |
Hi Ulrich, I think i am lost somewhere in the code for making the triangle as an template. For your reference, Please find the source code which i had created for making a triangle as an template and drawing the same using the template. I think now the functionality is totally wierd. I believe i had not implemented as you said. Can you pls point out what is the error i made on this. Thanks in advance! |
Sorry for the long wait. Had to fiddle around myself. SVG.Triangle = SVG.invent({
// Initialize node
create: function(){
this.constructor.call(this, SVG.create('polygon'))
this.type = 'triangle'
}
// Inherit from
, inherit: SVG.Shape
// Add parent method
, construct: {
// Create a wrapped polygon element
triangle: function(p) {
return this.put(new SVG.Triangle).plot(p)
}
}
}) In the next step I was mistaken, that you can just take the rect-drawing stuff and use it. SVG.Element.prototype.draw.extend('ellipse', {
init:function(e){
// We start with a circle with radius 1 at the position of the cursor
var p = this.startPoint;
this.el.attr({ cx: p.x, cy: p.y, rx: 1, ry: 1 });
},
calc:function (e) {
var p = this.transformPoint(e.clientX, e.clientY);
var ellipse = {
cx: this.startPoint.x,
cy: this.startPoint.y,
rx: Math.abs(p.x - this.startPoint.x),
ry: Math.abs(p.y - this.startPoint.y)
};
this.snapToGrid(ellipse);
this.el.attr(ellipse);
}
});
SVG.Element.prototype.draw.extend('triangle', {
init:function(e){
var p = this.startPoint;
this.el.plot([[p.x-2,p.y],[p.x-1,p.y-1],[p.x,p.y]]);
},
calc:function (e) {
var rect = {
x: this.startPoint.x,
y: this.startPoint.y
}, p = this.transformPoint(e.clientX, e.clientY);
rect.width = p.x - rect.x;
rect.height = p.y - rect.y;
// Snap the params to the grid we specified
this.snapToGrid(rect);
// When width is less than one, we have to draw to the left
// which means we have to move the start-point to the left
if (rect.width < 1) {
rect.x = rect.x + rect.width;
rect.width = -rect.width;
}
// ...same with height
if (rect.height < 1) {
rect.y = rect.y + rect.height;
rect.height = -rect.height;
}
// needed or math will explode
if(rect.width < 1 || rect.height < 1) return
this.el.move(rect.x,rect.y)
this.el.size(rect.width, rect.height)
}
}); Also you have to make sure, that you templateDraw.on('mouseup', function(event){
triangleTemp.draw('stop', event);
}); This way it works for me. And now to another topic: Instead of uploading your whole project here please create a fiddle. That has several advantages. |
Hi Ulrich,
I would like to ask one question.
I want to draw a triangle using the draw function.
But i dont want the user to click three points in the canvas for creating the triangle.
Instead of that i would like to create a traingle as template. but only as the user expands it the traingle should get expanded.
Is there any way for doing the same.
Thanks in advance.
The text was updated successfully, but these errors were encountered: