[英]Understanding placing images in Kinetic.JS?
Below I have some code for an assignment I'm working on. 下面有一些我正在处理的作业的代码。 Having been placed in a 2nd year course without taking foundations of JavaScript, I'm kind of in a rut.
在不使用JavaScript的基础上就读了第二年的课程,我有点发情。
I'm trying to place three images, and be able to call on them later with functions to change their opacity. 我正在尝试放置三张图像,并稍后可以使用函数来更改它们的不透明度。
When I have the images placed, only one seems to want to appear at a time, and when I call to them, I'm told that they don't exist. 当我放置图像时,似乎一次只想要出现一个图像,当我打电话给它们时,被告知它们不存在。
// Create the canvas based on the existing div for it in the HTML file
var stage = new Kinetic.Stage
({
container: 'canvasContainer',
width: 600,
height: 600
});
/* /////////////////////////////////////////////////////
C O D E F O R L O A D I N G U P I M A G E S
///////////////////////////////////////////////////// */
var characters = new Kinetic.Layer();
stage.add(characters);
window.onload = function()
{
var majoraCharacter = new Image();
majoraCharacter.src ="https://upload.wikimedia.org/wikipedia/commons/3/34/Red_star.svg"
majoraCharacter.onload = function()
{
character1= new Kinetic.Image({ x: 400, y: 300, width: 150, height: 150, offset: {x: 75, y: 75}, image: majoraCharacter});
characters.add(character1);
characters.draw();
}
}
window.onload = function()
{
var amaterasuCharacter = new Image();
amaterasuCharacter.src ="https://upload.wikimedia.org/wikipedia/commons/3/34/Red_star.svg"
amaterasuCharacter.onload = function()
{
character2= new Kinetic.Image({ x: 400, y: 200, width: 150, height: 150, offset: {x: 75, y: 75}, opacity:0.5, image: amaterasuCharacter});
characters.add(character2);
characters.draw();
}
}
window.onload = function()
{
var toothlessCharacter = new Image();
toothlessCharacter.src ="https://upload.wikimedia.org/wikipedia/commons/3/34/Red_star.svg"
toothlessCharacter.onload = function()
{
character3= new Kinetic.Image({ x: 400, y: 100, width: 150, height: 150, offset: {x: 75, y: 75}, image: toothlessCharacter});
characters.add(character3);
characters.draw();
}
}
/* /////////////////////////////////////////////////////
C A N V A S F U N C T I O N A L I T Y
///////////////////////////////////////////////////// */
// Create new layer for background images
// Create layer for character images
var character = new Kinetic.Layer();
var rect = new Kinetic.Rect({
x: 100,
y: 100,
width: 150,
height: 150,
fill: 'grey',
strokeWidth: 4,
offset: {x: 75, y: 75},
});
// add the shape to the layer
character.add(rect);
// add the layer to the stage
stage.add(character);
Is there a way I can better add images to canvas? 有没有一种方法可以更好地将图像添加到画布?
And what is preventing them from co-existing, and being editable outside of their loading functions? 是什么阻止它们共存并在加载功能之外进行编辑?
Any and all answers are greatly appreciated! 任何答案都将不胜感激!
A Demo: http://jsfiddle.net/m1erickson/Bf88D/ 演示: http : //jsfiddle.net/m1erickson/Bf88D/
Some KineticJS basics: 一些KineticJS基础知识:
Since you're new to javascript, here's an outline of how to structure your project 由于您不熟悉javascript,因此这里概述了如何构建项目
Here's an image loader that loads all your images in advance and then calls the start
function when your images are fully loaded. 这是一个图像加载器,它可以预先加载所有图像,然后在图像完全加载后调用
start
函数。
// image loader
var imageURLs=[]; // put the paths to your images here
var imagesOK=0;
var imgs=[];
imageURLs.push("");
loadAllImages(start);
function loadAllImages(callback){
for (var i=0; i<imageURLs.length; i++) {
var img = new Image();
imgs.push(img);
img.onload = function(){
imagesOK++;
if (imagesOK>=imageURLs.length ) {
callback();
}
};
img.onerror=function(){alert("image load failed");}
img.crossOrigin="anonymous";
img.src = imageURLs[i];
}
}
function start(){
// the imgs[] array holds fully loaded images
// the imgs[] are in the same order as imageURLs[]
// use the images now!
}
After you have all your images loaded, you can use this reusable function to add Kinetic Images on the layer. 加载所有图像后,可以使用此可重复使用的功能在图层上添加Kinetic Images。
This function adds one new image to the layer 此功能将一个新图像添加到图层
function addKineticImage(image,x,y,w,h){
var image = new Kinetic.Image({
x:x,
y:y,
width:w,
height:h,
image:image,
draggable: true
});
layer.add(image);
layer.draw();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.