如何使用 google-closure 使图形元素可拖动?

发布于 2024-08-22 05:17:42 字数 698 浏览 8 评论 0原文

如何使谷歌关闭图形元素可拖动并响应事件?

这是我到目前为止所拥有的。我有圆圈,但还不能拖动它:)。

谢谢。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半城柳色半声笛 2024-08-29 05:17:42

我必须在自己的项目中使用拖动器,但无法让 goog.fx.Dragger 工作。但是,我实现了自己的可拖动。它实际上要小得多并且非常简单。这里有一个要点:

  var graphic = new goog.graphics.ext.Graphics(1000, 500);
  var group = new goog.graphics.ext.Group(graphic);
  group.setLeft(20, true);
  group.setTop(20, true);
  group.setWidth(600, true);
  group.setHeight(200);      

  var fill = new goog.graphics.SolidFill('yellow');
  var stroke = new goog.graphics.Stroke(2, 'green');

  var bg = new goog.graphics.SolidFill('#eeeeee');
  var outline = new goog.graphics.Stroke(1, '#333333');

  var path = new goog.graphics.ext.Path().moveTo(0, 0).lineTo(20, 0).
      lineTo(10, 20).close();

  var shape = new goog.graphics.ext.Shape(group, path);
  shape.setLeft(10, true);
  shape.setTop(10, true);
  shape.setWidth('10%', true);
  shape.setHeight('10%');
  shape.setStroke(stroke);
  shape.setFill(fill);

  var ellipse = new goog.graphics.ext.Ellipse(group);
  ellipse.setCenter(0, true);
  ellipse.setMiddle(0, true);      
  ellipse.setWidth(120, true);
  ellipse.setHeight(60);
  ellipse.setStroke(stroke);
  ellipse.setFill(fill);


    goog.events.listen(group.getWrapper(), 'mousedown', function(e) {
        group.startOffsetX = e.offsetX;
        group.startOffsetY = e.offsetY;
        group.dragging = true;
    });
    goog.events.listen(group.getWrapper(), 'mouseup', function(e) {
        group.dragging = false;
    });
    goog.events.listen(group.getWrapper(), 'mousemove', function(e) {
        if(group.dragging) {
            group.setPosition(group.getLeft() + (e.offsetX-group.startOffsetX),
                              group.getTop()  + (e.offsetY-group.startOffsetY));
            group.startOffsetX = e.offsetX;
            group.startOffsetY = e.offsetY;
        };
    });
    goog.events.listen(group.getWrapper(), 'mouseout', function(e) {
        group.dragging = false;
    });
    graphic.render(document.body);

您当然也可以通过聆听它而不是组本身来聆听任何单个形状(矩形/椭圆形甚至路径)。我觉得这种方法为您提供了更大的灵活性(考虑将整个组的移动限制在画布或某些自定义范围内!)
我故意省略了 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:

  var graphic = new goog.graphics.ext.Graphics(1000, 500);
  var group = new goog.graphics.ext.Group(graphic);
  group.setLeft(20, true);
  group.setTop(20, true);
  group.setWidth(600, true);
  group.setHeight(200);      

  var fill = new goog.graphics.SolidFill('yellow');
  var stroke = new goog.graphics.Stroke(2, 'green');

  var bg = new goog.graphics.SolidFill('#eeeeee');
  var outline = new goog.graphics.Stroke(1, '#333333');

  var path = new goog.graphics.ext.Path().moveTo(0, 0).lineTo(20, 0).
      lineTo(10, 20).close();

  var shape = new goog.graphics.ext.Shape(group, path);
  shape.setLeft(10, true);
  shape.setTop(10, true);
  shape.setWidth('10%', true);
  shape.setHeight('10%');
  shape.setStroke(stroke);
  shape.setFill(fill);

  var ellipse = new goog.graphics.ext.Ellipse(group);
  ellipse.setCenter(0, true);
  ellipse.setMiddle(0, true);      
  ellipse.setWidth(120, true);
  ellipse.setHeight(60);
  ellipse.setStroke(stroke);
  ellipse.setFill(fill);


    goog.events.listen(group.getWrapper(), 'mousedown', function(e) {
        group.startOffsetX = e.offsetX;
        group.startOffsetY = e.offsetY;
        group.dragging = true;
    });
    goog.events.listen(group.getWrapper(), 'mouseup', function(e) {
        group.dragging = false;
    });
    goog.events.listen(group.getWrapper(), 'mousemove', function(e) {
        if(group.dragging) {
            group.setPosition(group.getLeft() + (e.offsetX-group.startOffsetX),
                              group.getTop()  + (e.offsetY-group.startOffsetY));
            group.startOffsetX = e.offsetX;
            group.startOffsetY = e.offsetY;
        };
    });
    goog.events.listen(group.getWrapper(), 'mouseout', function(e) {
        group.dragging = false;
    });
    graphic.render(document.body);

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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文