如何使用 google-closure 使图形元素可拖动?
如何使谷歌关闭图形元素可拖动并响应事件?
这是我到目前为止所拥有的。我有圆圈,但还不能拖动它:)。
谢谢。
goog.require('goog.dom');
goog.require('goog.graphics');
goog.require('goog.events');
goog.require('goog.fx.Dragger');
goog.provide('graphics_test');
graphics_test = function(){
var canvas = goog.dom.createDom('div', {'id':'canvas'});
goog.dom.appendChild(document.body, canvas);
var g = goog.graphics.createGraphics(600,400);
var fill = new goog.graphics.SolidFill('yellow');
var stroke = new goog.graphics.Stroke(1,'black');
circle = g.drawCircle(300, 200, 50, stroke, fill);
var dragger = new goog.fx.Dragger(circle,circle);
g.render(goog.dom.$('canvas'));
};
How to make google closure graphics elements draggable and respond to events otherwise?
Here's what I have so far. I have the circle, but can't drag it yet :).
Thanks.
goog.require('goog.dom');
goog.require('goog.graphics');
goog.require('goog.events');
goog.require('goog.fx.Dragger');
goog.provide('graphics_test');
graphics_test = function(){
var canvas = goog.dom.createDom('div', {'id':'canvas'});
goog.dom.appendChild(document.body, canvas);
var g = goog.graphics.createGraphics(600,400);
var fill = new goog.graphics.SolidFill('yellow');
var stroke = new goog.graphics.Stroke(1,'black');
circle = g.drawCircle(300, 200, 50, stroke, fill);
var dragger = new goog.fx.Dragger(circle,circle);
g.render(goog.dom.$('canvas'));
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我必须在自己的项目中使用拖动器,但无法让 goog.fx.Dragger 工作。但是,我实现了自己的可拖动。它实际上要小得多并且非常简单。这里有一个要点:
您当然也可以通过聆听它而不是组本身来聆听任何单个形状(矩形/椭圆形甚至路径)。我觉得这种方法为您提供了更大的灵活性(考虑将整个组的移动限制在画布或某些自定义范围内!)
我故意省略了 goog.events.listen 中的第五个参数(opt_handler),以使此代码更具可读性。
希望这有帮助:)
I had to use a dragger in my own project and couldn't get goog.fx.Dragger to work. However, I implemented my own draggable. Its actually much smaller and pretty simple. Here is a gist:
You can of course listen on any single shape too (rectangle/ellipse or even a path) by listening to it instead of the group itself. I feel this method gives you more flexibility (think restricting the movement of the entire group to the canvas or some custom bounds!)
I purposely omitted the fifth argument(opt_handler) from goog.events.listen so as to make this code more readable.
Hope this helps :)