javascript - createjs html5 canvas 集成不起作用

标签 javascript canvas createjs

我试图将 w3school html5 canvas 与 flash export createjs 集成 我刚刚在 createjs 输出中添加了这段代码。

 var ctx = canvas.getContext("2d");
    ctx.fillStyle = "#FF0000";
    ctx.fillRect(0,0,150,75);

但没有显示错误。不工作............

<script>
var canvas, stage, exportRoot;

function init() {
    canvas = document.getElementById("canvas");
    exportRoot = new lib.Untitled1();

var canvas = document.getElementById("myCanvas");

var ctx = canvas.getContext("2d");
ctx.fillStyle = "#FF0000";
ctx.fillRect(0,0,150,75);

stage = new createjs.Stage(canvas);
stage.addChild(exportRoot);
stage.update();
    createjs.Ticker.setFPS(24);
    createjs.Ticker.addEventListener("tick", stage);
}
</script>

最佳答案

在您的示例中,您将内容绘制到 Canvas 上下文,然后更新使用相同上下文的 EaselJS 阶段。默认情况下,EaselJS 会在绘制之前清除上下文。您可以关闭自动清除,但这会产生其他问题。

更好的解决方案是使用 EaselJS 来绘制其他内容。

// Create your red square
var shape = new createjs.Shape();
shape.graphics.beginFill("#FF0000").drawRect(0,0,150,75);

// Create the stage and add the exportRoot
stage = new createjs.Stage(canvas);
stage.addChild(exportRoot);

// Add your shape to the stage
stage.addChild(shape);

// Then the rest...
stage.update();
createjs.Ticker.setFPS(24);
createjs.Ticker.addEventListener("tick", stage);

如果您需要使用自己的上下文操作,可以将它们绘制到单独的 Canvas 上,然后使用该 Canvas 作为位图的源

var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
ctx.fillStyle = "#FF0000";
ctx.fillRect(0,0,150,75);
var bitmap = new createjs.Bitmap(canvas);

stage = new createjs.Stage("otherCanvasId");
stage.addChild(exportRoot);
stage.addChild(bitmap);
// Other stuff here

希望这是有道理的!

关于javascript - createjs html5 canvas 集成不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37548856/

相关文章:

java - 为什么我的小程序创建一个单独的窗口

javascript - TweenJS 动画不起作用

javascript - 在 url 验证中禁用 @ 符号

javascript - javascript 片段是什么意思?

html - 防止在 html5 canvas touchmove 事件期间滚动

javascript - CreateJS 使用 Tween.js 增加阴影 offsetX

javascript - HTML5 Canvas /闪光灯。如何访问 child 电影剪辑并使其转到AndPlay?

javascript - jQuery 将 OPTIONS 而不是 POST 请求发送到本地主机上的 REST

javascript - 如何使用AngularJS的多个ajax调用($http)从单个php文件请求数据?

javascript - 如何在 mailto 链接中附加 Canvas 图像?