使用preloadjs加载图像时如何在createjs中更改位图图像?
How to change bitmap image in createjs when images are loaded using preloadjs?
我使用队列中的图像创建了位图。接下来,我试图通过更新 image.src 属性 来更改图像,但它不起作用,因为它需要一条通往图像位置的路径。在这种情况下如何将第一张图像更改为第二张图像。我假设过渡会很顺利,因为图像已经加载。
var queue = new createjs.LoadQueue();
queue.on("complete", loaded);
queue.loadManifest([{
id: "one",
src: "image1.png"
},
{
id: "two",
src: "image2.png"
}
]);
var stage = new createjs.Stage('canvas'),
img1 = queue.getResult("one"),
img2 = queue.getResult("two");
var changingImage = new createjs.Bitmap(img1);
stage.addChild(childImage);
stage.update();
function changeImage() {
changingImage.image.src = img2;
stage.update();
}
调用函数 changeImage() 时,图像必须在 changingImage 中发生变化。
我做了一个你可以使用的小例子:)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Example</title>
<script src="https://code.createjs.com/createjs-2015.11.26.min.js"></script>
<script>
var stage,queue;
function init()
{
stage = new createjs.Stage("canvas");
createjs.Ticker.setFPS(12); //set some FPS
createjs.Ticker.addEventListener("tick", stage); //set autiomatic refresh, so we don't need to use stage.update() frequently
queue = new createjs.LoadQueue();
queue.on("complete", loaded);
queue.loadManifest(
[
{
id: "one",
src: "image1.png"
},
{
id: "two",
src: "image2.png"
}
]);
}
function loaded()
{
var img1 = queue.getResult("one");
var img2 = queue.getResult("two");
var someBitmap = new createjs.Bitmap(img1);
stage.addChild(someBitmap); //first image is visible now
//let's wait for a second and then call the function
//img2 is reference to the second image
setTimeout(function(){changeImage(someBitmap,img2)},1000);
}
function changeImage(bitmap,img)
{
bitmap.image=img; //so image is changed
}
</script>
</head>
<body onload="init();">
<canvas id="canvas" width="600" height="400"></canvas>
</body>
</html>
我使用队列中的图像创建了位图。接下来,我试图通过更新 image.src 属性 来更改图像,但它不起作用,因为它需要一条通往图像位置的路径。在这种情况下如何将第一张图像更改为第二张图像。我假设过渡会很顺利,因为图像已经加载。
var queue = new createjs.LoadQueue();
queue.on("complete", loaded);
queue.loadManifest([{
id: "one",
src: "image1.png"
},
{
id: "two",
src: "image2.png"
}
]);
var stage = new createjs.Stage('canvas'),
img1 = queue.getResult("one"),
img2 = queue.getResult("two");
var changingImage = new createjs.Bitmap(img1);
stage.addChild(childImage);
stage.update();
function changeImage() {
changingImage.image.src = img2;
stage.update();
}
调用函数 changeImage() 时,图像必须在 changingImage 中发生变化。
我做了一个你可以使用的小例子:)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Example</title>
<script src="https://code.createjs.com/createjs-2015.11.26.min.js"></script>
<script>
var stage,queue;
function init()
{
stage = new createjs.Stage("canvas");
createjs.Ticker.setFPS(12); //set some FPS
createjs.Ticker.addEventListener("tick", stage); //set autiomatic refresh, so we don't need to use stage.update() frequently
queue = new createjs.LoadQueue();
queue.on("complete", loaded);
queue.loadManifest(
[
{
id: "one",
src: "image1.png"
},
{
id: "two",
src: "image2.png"
}
]);
}
function loaded()
{
var img1 = queue.getResult("one");
var img2 = queue.getResult("two");
var someBitmap = new createjs.Bitmap(img1);
stage.addChild(someBitmap); //first image is visible now
//let's wait for a second and then call the function
//img2 is reference to the second image
setTimeout(function(){changeImage(someBitmap,img2)},1000);
}
function changeImage(bitmap,img)
{
bitmap.image=img; //so image is changed
}
</script>
</head>
<body onload="init();">
<canvas id="canvas" width="600" height="400"></canvas>
</body>
</html>