如何将 Backbone.js 单击事件绑定到 HTML5 画布内呈现的视图?

发布于 2024-11-01 03:11:46 字数 536 浏览 1 评论 0原文

我刚刚开始使用 Backbone.js,但它看起来真的很有趣......

现在,我正在重做以前的项目将各种对象(2-3 种不同的模型类型)绘制到单个 HTML5 画布中。

每个对象都是可点击的。画布有一个事件处理程序,用于获取单击的位置(画布对象的本地位置),然后在对象中搜索可能产生点击的对象。

在对 Backbone.js 视图上的单击事件执行此操作时,是否应该使用特定的方法或最佳实践?

谢谢!

更新:发现fabric.js它似乎处理画布元素中的对象的想法,但不提供像backbone.js这样的MVC风格框架。

另外,我还查看了 knockout.js。它似乎比backbone.js 更依赖于HTML 元素(而不是canvas)。

I am just getting started with Backbone.js, but it looks really interesting...

Right now, I am redoing a previous project that draws various objects (2-3 different model types) into a single HTML5 canvas.

Each object is clickable. There is a event handler for the canvas that gets the location of the click (local to the canvas object) and then searches the objects for one that could produce a hit.

Is there a particular way or best practice that I should use when doing this for a click event on a Backbone.js view?

Thanks!

Update: found fabric.js which seems to handle the idea of objects within a canvas element, but doesn't provide the MVC style framework as backbone.js.

Also, I took a look at knockout.js. It seems even more tied to HTML elements (not canvas) than backbone.js.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

太阳哥哥 2024-11-08 03:11:46

我遇到这个问题是因为我正在开发一个基于 Backbone 的画布框架,所以我自己解决了这个问题。我最终得出的答案是:忘记在画布上使用 Backbone.View。如果它不能用 DOM 元素表示,那么使用 Backbone.View 就没有意义。 ;它的属性只是不映射到画布“元素”。

请注意,您可以使用 Backbone.View 来表示单个 canvas DOM 元素,但这不是您所描述的场景。您需要的是一个自定义视图类,它可以表示您的画布内对象并处理它们的事件。

I came across this question because I'm working on a Backbone-based framework for working with canvas, so I grappled with this myself. The answer I eventually came to was: Forget about using Backbone.View with canvas. If it can't be represented by a DOM element, then it doesn't make sense to use Backbone.View for it; its properties just don't map to canvas "elements."

You could use Backbone.View to represent a single canvas DOM element just fine, mind you, but that's not the scenario you're describing. What you need is a custom view class that can represent your in-canvas objects and handle their events.

阪姬 2024-11-08 03:11:46

我还在 Canvas 上使用 Backbone.js 开发纸牌游戏,并遇到了这个问题。我目前正在做的是有一个 CardView ,它有 el 作为画布标签,以便在初始化时创建画布。卡片的所有绘制都是在此视图的 render 方法上完成的。但是,此画布不会添加到 DOM 中。

然后我有一个 Hand,它是一个 Backbone.Collection 来保存卡片集合。该集合有一个附加到游戏画布的 Backbone.ViewDOM 中唯一且较大的画布)。该视图还包含一个 Backbone.View 数组。然后我有:

render: function() {
    var that = this;
    this.ctx.save();
    _(this._views).each(function(view) {
        that.ctx.drawImage(view.render().el, 0, 0);
        that.ctx.translate(view.el.width, 0);
    });
    this.ctx.restore();
},

基本上,我使用上下文的 drawImage 将每个视图的画布绘制到游戏画布中。

我将 addEventListener 添加到 HandView 的画布(即它的 el)来处理点击。在此处理程序中,我检查 CardView 的鼠标位置,然后操作该视图。

请注意,这只是我正在尝试的东西,因为我也是 Backbone.js 的新手。这可能是最糟糕的方法,但我只是希望您能看到其中一种方法。

我确实希望我们能够针对 DOM 中不存在的画布检查鼠标。如果可能的话,我们可以将 addEventListener 添加到 CardView 的画布上,并让它进行翻译和重新渲染。 HandView 的画布不需要循环浏览其视图。

I am also working on a card game using Backbone.js on Canvas and came across this question. What I am currently doing is have a CardView that has el as a canvas tag so that a canvas will be created on initialization. All the drawing of the card is done on this view's render method. However, this canvas will not be added to the DOM.

I then have a Hand, which is a Backbone.Collection to hold a collection of cards. This collection has a Backbone.View attached to the game canvas (the only and big one in the DOM). This view also holds an array of Backbone.View's. Then I have:

render: function() {
    var that = this;
    this.ctx.save();
    _(this._views).each(function(view) {
        that.ctx.drawImage(view.render().el, 0, 0);
        that.ctx.translate(view.el.width, 0);
    });
    this.ctx.restore();
},

Basically, I draw each view's canvas into the game canvas using drawImage of the context.

I addEventListener to HandView's canvas (which is its el) to handle click. In this handler, I check the mouse positions against which CardView, then manipulate that view.

Mind you, this is just something I'm experimenting since I'm new to Backbone.js too. This maybe the worst way to do, but I just hope that you can see one of the ways to do it.

I do wish that we could check mouse against a canvas that does not exists in the DOM. If it's possible, we could just addEventListener to CardView's canvas and let it translate and re-render. No need for HandView's canvas to loop through its views.

执妄 2024-11-08 03:11:46

我参加这个聚会迟到了,但在我看来,视图和模型/集合都不是容纳这种逻辑的地方。

如果您使用的是 Fabric 或其他一些库(我有一个使用 raphael.js 的应用程序),您应该创建一个定制模块来处理绘图/操作并使用本地事件处理响应画布事件(即 https://github.com/kangax/fabric.js/wiki/Working-with-events )。

如果您需要传达这些事件,您应该以编程方式公开它们,并使用主干事件将它们发布到其他主干组件。

您不应该按照上面建议的方式将模型和视图紧密耦合。

I'm late to this party, but IMO neither views nor models/collections are not the place to house this kind of logic.

If you're using fabric or some other library (I have an application using raphael.js), you should be creating a bespoke module that handles the drawing/manipulation and responds to canvas events using local events handling (ie. https://github.com/kangax/fabric.js/wiki/Working-with-events).

If you need to communicate those events out you should expose them programatically and use backbone events to publish them to other backbone components.

You shouldn't tightly couple your models and views in the ways suggested above.

无人接听 2024-11-08 03:11:46

我自己刚刚开始使用backbone.js,但我可能会制作一个主干视图(例如canvasView)来渲染和侦听画布上的事件。单击事件可以分派到视图对象上的方法,并在该方法中执行对象匹配算法和其他逻辑。

Just getting started with backbone.js myself, but I would probably make a backbone view (say canvasView) that renders and listens to the events on the canvas. The click event can be dispatched to a method on the view objects and in that method you do your object-matching algorithm and other logic.

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