简体   繁体   English

如何在JavaScript类中正确使用HTML5的画布?

[英]How to properly use HTML5's canvas within JavaScript classes?

First off, I use the term "classes" to mean functions with prototypes that might be in a separate file from the main initializing file for what I'm working on. 首先,我使用术语“类”来表示具有原型的功能,这些功能可能与我正在处理的内容与主要的初始化文件位于单独的文件中。

Now for my question/issue: I'm working on building something in JavaScript/HTML5, and trying to program it "properly" (ie. using prototypes with formats that are, I hope, standard). 现在,我的问题是:我正在使用JavaScript / HTML5构建某些东西,并尝试对其进行“适当”编程(即,使用我希望标准格式的原型)。 In my main JavaScript file, I have methods that have create use an instance (basically the root instance of my OOP/prototype based script) that sets up the canvas. 在我的主要JavaScript文件中,我拥有一些使用创建画布的实例(基本上是基于OOP /原型的脚本的根实例)创建的方法。

I also have another file that is loaded which contains a 'class' for creating clickable buttons. 我还加载了另一个文件,其中包含用于创建可点击按钮的“类”。 As of right now, I'm just trying to get the buttons to draw on the canvas, however, I can't access the instance of the canvas because I believe the instance is out of scope (which is fine, I don't want everything I do to contain a long dot-notation for referencing instances). 截至目前,我只是试图在画布上绘制按钮,但是,我无法访问画布的实例,因为我认为实例不在范围内(很好,我没有希望我所做的一切都包含用于引用实例的长点符号)。 However, this is giving me trouble when trying to draw a rectangle on the canvas, considering the 'class' the button is in, isn't able to reference the context outside of it. 但是,考虑到按钮所在的“类”,无法在其外部引用上下文时,这在尝试在画布上绘制矩形时给了我麻烦。

I found a way around this by using something along the lines of: 我通过以下方法找到了解决此问题的方法:

function CreateButton(btn_x, btn_y, btn_width, btn_height, btn_txt) {

    // ... (check and define parameters)

    CreateButton.prototype.custom_canvas = document.getElementById('root_canvas');
    CreateButton.prototype.ctxt = this.custom_canvas.getContext('2d');

    CreateButton.prototype.ctxt.fillStyle = '#666666';
    CreateButton.prototype.ctxt.fillRect(this.x, this.y, this.width, this.height);
}

Basically, it's writing on top of the canvas with a canvas of the same name? 基本上,它是在画布上用同名的画布书写的吗? I'd assume that I can still manipulate the regular canvas afterwards and it would just act as if it was a single canvas element. 我假设以后我仍然可以操作常规画布,并且它的作用就像是一个画布元素一样。 I worried that redrawing on the canvas might use up a lot of memory when many things are added, however, no matter the method, writing on top of the canvas can't be avoided (even when in the same scope). 我担心在添加许多内容后在画布上重画可能会占用大量内存,但是,无论采用哪种方法,都无法避免在画布顶部进行书写(即使在同一范围内)。

Any suggestions, or help? 有什么建议或帮助吗? Or is my method of using the same canvas within a different class acceptable? 还是我在不同类中使用相同画布的方法可以接受?

Thanks for any feedback. 感谢您的任何反馈。

[UPDATE] Hmm, maybe I should try passing the context as a parameter and just using that. [更新]嗯,也许我应该尝试将上下文作为参数传递并使用它。

...Or should I just make the canvas a global object? ...还是应该只将画布设为全局对象? Any suggestions? 有什么建议么?

I guess you could try to implement some sort of "WidgetManager" that retains reference to canvas and your widgets. 我猜您可以尝试实现某种“ WidgetManager”,以保留对画布和小部件的引用。 It will use a callback mechanism to render widgets. 它将使用回调机制来呈现小部件。 Each widget (ie. in this case Button) will have certain kind of rendering states (pressed, released) and some kind of internal state. 每个小部件(即本例中的Button)将具有某种类型的呈现状态(按下,释放)和某种内部状态。 Other widgets might have more complicated states. 其他小部件的状态可能更复杂。

Note that "WidgetManager" should probably keep track of widget "z" and user presses (which widget was hit). 请注意,“ WidgetManager”可能应该跟踪小部件“ z”和用户按下的操作(命中哪个小部件)。 Based on this it should be able to trigger handlers bound to widgets. 基于此,它应该能够触发绑定到小部件的处理程序。 In a way you have to reinvent what basic UI libs do already. 在某种程度上,您必须重新发明基本的UI库已经完成的工作。 :) :)

I think you are better off by working out your design this way before moving into the implementation phase. 我认为,最好在进入实施阶段之前通过这种方式进行设计,才能更好。 A lot depends on what you really want to with it. 在很大程度上取决于您真正想要的是什么。 :) :)

Note that you can simplify rendering and checks a lot by using multiple canvasii instead of just one. 请注意,您可以使用多个canvasii而不是一个来简化渲染并进行很多检查。 In this case you'll have to deal with z-index and absolute positioning but at least you get to piggyback on some of the existing stuff without having to implement it yourself. 在这种情况下,您将不得不处理z-index和绝对定位,但是至少您可以piggy带一些现有的东西而不必自己实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM