简体   繁体   English

是否了解在Kinetic.JS中放置图片?

[英]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基础知识:

  • KineticJS starts with a stage. KineticJS从一个阶段开始。
  • The stage is a container for one or more layers which are added to the stage 舞台是用于一个或多个添加到舞台的层的容器
  • Each layer is actually a canvas 每层实际上是一块画布
  • Shapes (including images) are added to a layer. 形状(包括图像)将添加到图层。

Since you're new to javascript, here's an outline of how to structure your project 由于您不熟悉javascript,因此这里概述了如何构建项目

  • just use one window.onload and put all your javascript in that one window.onload 只需使用一个window.onload并将您所有的javascript放在一个window.onload中
  • load all images in advance so they are available to draw on your canvas 预先加载所有图像,以便可以在画布上绘制它们
  • When all the images are loaded, then create Kinetic.Image objects from them. 加载所有图像后,从它们创建Kinetic.Image对象。

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。

  • You send in one image that you've loaded in advance. 您发送一张预先加载的图像。
  • You also send in the x,y position where you want the image located. 您还可以将x,y位置发送到想要放置图像的位置。
  • You also send in the width,height that you want the image resized to. 您还发送想要调整图像大小的宽度,高度。

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.

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