dojox.drawing.Drawing - 用于创建圆角矩形的自定义工具
我正在使用 dojox.drawing.Drawing 创建一个简单的图表工具。我创建了一个自定义工具来通过扩展 dojox.drawing.tools.Rect 来绘制圆角矩形,如下所示 -
dojo.provide("dojox.drawing.tools.custom.RoundedRect");
dojo.require("dojox.drawing.tools.Rect");
dojox.drawing.tools.custom.RoundedRect = dojox.drawing.util.oo.declare(
dojox.drawing.tools.Rect,
function(options){
},
{
customType:"roundedrect"
}
);
dojox.drawing.tools.custom.RoundedRect.setup = {
name:"dojox.drawing.tools.custom.RoundedRect",
tooltip:"Rounded Rect",
iconClass:"iconRounded"
};
dojox.drawing.register(dojox.drawing.tools.custom.RoundedRect.setup, "tool");
我能够将我的工具添加到工具栏并使用它在画布上绘制矩形。现在,我想将自定义工具创建的矩形自定义为具有圆角,但我不知道如何实现。 我已经检查了 dojox.drawing.tools.Rect 类的源代码以及它的父 dojox.drawing.stencil.Rect 类,我可以看到在 dojox.drawing.stencil.Rect 中创建的实际矩形,如下所示 -
_create: function(/*String*/shp, /*StencilData*/d, /*Object*/sty){
// summary:
// Creates a dojox.gfx.shape based on passed arguments.
// Can be called many times by implementation to create
// multiple shapes in one stencil.
//
//console.log("render rect", d)
//console.log("rect sty:", sty)
this.remove(this[shp]);
this[shp] = this.container.createRect(d)
.setStroke(sty)
.setFill(sty.fill);
this._setNodeAtts(this[shp]);
}
在 dojox 中。 gfx,可以通过设置 r 属性将圆角添加到矩形。 在这种情况下,有人可以回答我的以下问题吗?
- dojox.drawing 中自定义矩形外观的机制是什么 圆角?
- 在上面的代码片段中,StencilData 被传递给 createRect 调用。定制这些数据的机制是什么?可以在此数据中设置控制圆角的矩形的 r 属性吗?
I'm working with dojox.drawing.Drawing to create a simple diagramming tool. I have created a custom tool to draw rounded rectangle by extending dojox.drawing.tools.Rect as shown below -
dojo.provide("dojox.drawing.tools.custom.RoundedRect");
dojo.require("dojox.drawing.tools.Rect");
dojox.drawing.tools.custom.RoundedRect = dojox.drawing.util.oo.declare(
dojox.drawing.tools.Rect,
function(options){
},
{
customType:"roundedrect"
}
);
dojox.drawing.tools.custom.RoundedRect.setup = {
name:"dojox.drawing.tools.custom.RoundedRect",
tooltip:"Rounded Rect",
iconClass:"iconRounded"
};
dojox.drawing.register(dojox.drawing.tools.custom.RoundedRect.setup, "tool");
I was able to add my tool to the toolbar and use it to draw a rectagle on canvas. Now, I would like to customize the rectangle created by my custom tool to have rounded corners, but I'm not able to figure out how.
I have checked the source of dojox.drawing.tools.Rect class as well as it's parent dojox.drawing.stencil.Rect class and I can see the actual rectangle being created in dojox.drawing.stencil.Rect as follows -
_create: function(/*String*/shp, /*StencilData*/d, /*Object*/sty){
// summary:
// Creates a dojox.gfx.shape based on passed arguments.
// Can be called many times by implementation to create
// multiple shapes in one stencil.
//
//console.log("render rect", d)
//console.log("rect sty:", sty)
this.remove(this[shp]);
this[shp] = this.container.createRect(d)
.setStroke(sty)
.setFill(sty.fill);
this._setNodeAtts(this[shp]);
}
In dojox.gfx, rounded corners can be added to a a rectangle by setting r property.
With this context, could anybody please provide answers to my following questions?
- What's the mechanism in dojox.drawing to customize the appearance of rectangle to have
rounded corners? - In the code snippet above, StencilData is passed to createRect call. What's the mechanism to customize this data? Can the r property of a rectangle that governs rounded corners be set in this data?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以编程方式添加圆角矩形很容易。在测试文件夹中,您将找到 test_shadows.html,其中有一行添加了带圆角的矩形:
您创建一个具有 x、y、宽度、高度和 r 值(否则默认为 0)的数据对象。
如果您想通过扩展 rect 来做到这一点,最简单的方法就是在构造函数中设置值(例如 data.r=10),或者您可以创建一个pointsToData 函数来覆盖 Rect 的版本。您要么设置 this.data.r 的值,要么设置默认值:
在该示例中,我将 r 的默认值设置为 10,而不是之前的 0。这是有效的,因为每次模板去绘制矩形时,它都会将画布 x,y 点(所有模板都会记住它们的点)转换为数据(gfx 用于绘制)。换句话说,这个函数总是在 rect 渲染之前被调用。
Adding rounded rectangles programmatically is easy. In the tests folder you'll find test_shadows.html which has a line that adds a rectangle with rounded corners:
You create a data object with x,y,width,height, and a value for r (otherwise it defaults to 0).
If you wanted to do it by extending rect, the easiest way to do it would just be to set the value in the constructor function (data.r=10, for example), or you could create a pointsToData function to override Rect's version. Either you would have set the value for this.data.r, or the default:
In that example I give r the value 10 as the default, instead of 0 as it was before. This works because every time stencil goes to draw your rect, it converts canvas x,y points (all stencils remember their points) to data (which gfx uses to draw). In other words this function will always be called before rect renders.