简体   繁体   English

使用requirejs结构时,不显示画布中的画布

[英]canvas in canvas not displayed when requirejs structure used

I have a problem that is driving me insane. 我有一个让我发疯的问题。

I have something that works perfectly fine in one javascript file, but completely fails when using it with requirejs. 我有一个在一个JavaScript文件中可以正常工作的东西,但是与requirejs一起使用时完全失败。

(function(){
define([], function (){
    var Sprite = function(){
        return {
            spritesheet:    null,
            canvas:         null,
            context:        null,

            init: function(src, ctx){
                this.canvas = document.createElement('canvas');
                this.context = this.canvas.getContext('2d');
                this.context.drawImage(src, 0, 0, 10, 20, 0, 0, 10, 20);
                ctx.drawImage(this.canvas, 100, 100);
            }
        };
};

return Sprite;
});
}
)();

And in other file I have: 在其他文件中,我有:

(function(){
define(["r", "sprite", "player"], function (Ra, Sprite, Player){
    var Game = function()
    {
        return {
            ...SOME STUFF HERE...
            canvas: null,
            context: null,

            something: function() {
                this.canvas = document.getElementById("game");
                this.canvas.width = 500;
                this.canvas.height = 500;
                this.context = this.canvas.getContext("2d");

                var sprite = new Sprite();
                sprite.init("image.gif", this.context);
         };
     };

     return Game;
});
}
)();

I tried to compact the code as much as I can, I hope I didn't cut out anything important, but that is basically the part that is not working. 我试图尽可能地压缩代码,希望我没有删节任何重要的内容,但这基本上是不起作用的部分。 I am also fairy new to requirejs so if I am doing something incredibly wrong, please tell me. 我也是requirejs的新手,所以如果我做错了什么,请告诉我。

No matter what I do, everything seems to be fine, both canvases are created, all have the right properties, but the second one I am putting on the first one is not displayed. 无论我做什么,似乎一切都很好,两个画布都创建了,都具有正确的属性,但是我放置在第一个画布上的第二个画布没有显示。 If I put plain image instead of canvas it works just fine, but canvas on canvas does not. 如果我将普通图像替换为画布,则效果很好,但画布上的画布却不能。

This drives me nuts, I tried so many possibilities, the code works just fine without all the requirejs stuff, but fails when I structure it that way. 这让我发疯了,我尝试了很多可能性,没有所有requirejs的东西,代码也可以正常工作,但是当我以这种方式构造它时,它就会失败。 Does it have something to do with global scopes? 它与全局范围有关吗? How to make this work? 如何使这项工作? I need canvas inside canvas. 我需要帆布里面的帆布。

Thank you for your time! 感谢您的时间!

EDIT: 编辑:

I have only <script data-main="main" src="libs/require/require.js"></script> loaded in my html file and <canvas id="game"></canvas> in body. 我在HTML文件中仅加载了<script data-main="main" src="libs/require/require.js"></script> ,在正文中仅包含了<canvas id="game"></canvas>

Then in main.js I have: 然后在main.js中,我有:

require.config({ 
baseUrl: "./modules",
waitSeconds: 10,
packages: [
    {
        name: "something not used yet",
        location: "something not used yet",
        main: "something not used yet"
    }
]
});

require(['game'], function (Game)
{
    var game = new Game();
    game.begin();
});

Rest I already showed in the beginning. 我已经在一开始就显示了休息。

I have main.html and main.js together in the build folder and all the others in build/modules folder. 我在build文件夹中有main.html和main.js,在build / modules文件夹中有其他所有文件。

Problem was the most ridiculous thing ever and I wasted way to much time to figure out something this obvious... 问题是有史以来最荒谬的事情,我浪费了很多时间去弄清楚这种明显的事情……

For some reason I completely forgot to wait for the image to load and kept using it before it is even loaded, and the complicated (for me at least) structure when using requirejs made it hard to pinpoint that mistake. 出于某种原因,我完全忘记等待图像加载,甚至在图像加载之前就一直使用它,而使用requirejs时复杂的(至少对我来说)结构很难确定该错误。

Also without requirejs the code worked because image loaded so fast, it was ready before the next function started even without onload, however when I switched to requirejs I guess it either slowed down, or the asynchronous nature of it created the problem. 同样,在没有requirejs的情况下,代码也能正常工作,因为图像加载速度如此之快,即使没有onload也可以在下一个函数启动之前就准备好了,但是当我切换到requirejs时,我猜它要么变慢了,要么是它的异步特性造成了问题。

Anyway, solved by simple onload handler. 无论如何,可以通过简单的onload处理程序解决。

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

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