简体   繁体   English

使用JavaScript和KineticJS库在HTML5画布上以“网格”布局绘制图像

[英]Drawing images in a 'grid' layout on the HTML5 canvas using JavaScript and the KineticJS library

I have an array of images which I want to draw to the HTML5 canvas in specific locations (ie using a 'grid' type layout). 我有一个图像数组,希望在特定位置(例如,使用“网格”类型的布局)绘制到HTML5画布上。

I have a function called drawImage(imageObj) which currently draws all of the images in the array to random locations on the canvas. 我有一个名为drawImage(imageObj)的函数,该函数当前将数组中的所有图像绘制到画布上的随机位置。 However, I want to be able to predetermine a set number of locations at which the images should be drawn, and then choose which image is drawn at which of these set locations randomly (it doesn't matter if more than one image is drawn to the same location). 但是,我希望能够预先确定要绘制图像的位置的设定数量,然后选择在这些设置位置中的哪个位置随机绘制哪个图像(如果要绘制多个图像都没有关系相同的位置)。

My drawImage(imageObj) function currently looks like this: 我的drawImage(imageObj)函数当前如下所示:

function drawImage(imageObj) {
    var canvasImage = new Kinetic.Image({
      image: imageObj,
      width: 50,
      height: 50,



      /* puts the images in random locations on the canvas */
      x: stage.getWidth() / 20*Math.floor(Math.random()*20),
      y: stage.getHeight() / 15*Math.floor(Math.random()*8+2),
      draggable: true 
    });

    // add cursor styling
    canvasImage.on('mouseover', function() {
      document.body.style.cursor = 'pointer';
    });
    canvasImage.on('mouseout', function() {
      document.body.style.cursor = 'default';
    });

    imagesLayer.add(canvasImage);
}

I tried altering the function slightly to create a 'grid' of locations, and then draw each image to a random 'cell' in the grid: 我尝试稍微改变功能以创建位置的“网格”,然后将每个图像绘制到网格中的随机“单元”:

function drawImage(imageObj) {
    /* Create arrays of X & Y coordinates, and select the array elements randomly for use as
        coordinates at which to draw the images*/
    var xPos = new Array();
    xPos[0] = 10;
    xPos[1] = 70;
    xPos[2] = 130;
    xPos[3] = 190;
    xPos[4] = 250;
    xPos[5] = 310;
    xPos[6] = 370;
    xPos[7] = 430;
    xPos[8] = 490;
    xPos[9] = 550;
    xPos[10] = 610;
    xPos[11] = 670;
    xPos[12] = 730;
    xPos[13] = 790;
    xPos[14] = 850;
    xPos[15] = 910;

    var yPos = new Array();
    yPos[0] = 10;
    yPos[1] = 70;
    yPos[2] = 130;
    yPos[3] = 190;
    yPos[4] = 250;
    yPos[5] = 310;
    yPos[6] = 370;
    yPos[7] = 430;

    var canvasImage = new Kinetic.Image({
      image: imageObj,
      width: 50,
      height: 50,

      /* Now select a random X & Y position from the arrays to draw the images to */
      x: xPos(getRandomXPosition),
      y: yPos(getRandomYPosition),
      draggable: true

      /* puts the images in random locations on the canvas
      x: stage.getWidth() / 20*Math.floor(Math.random()*20),
      y: stage.getHeight() / 15*Math.floor(Math.random()*8+2),
      draggable: true */
    });

I had expected that the lines 我原本以为

x: xPos(getRandomXPosition),
y: yPos(getRandomYPosition),

would set the x and y coordinates of the image that is being drawn to the canvas to a random 'cell' in my 'grid' determined by which random elements of the xPos and yPos arrays were set as the x and y values of the image that was to be drawn. 会将绘制到画布上的图像的x和y坐标设置为我的“网格”中的随机“单元格”,该单元格由xPosyPos数组的哪些随机元素设置为图像的x和y值来确定那是要画的。

However, when I view my page in the browser, I'm getting a console error which says that "xPos is not a function" on the line 但是,当我在浏览器中查看页面时,出现控制台错误,该错误显示“ xPos不是函数”

x: xPos(getRandomXPosition),

I can't figure out why this is- does anyone have any ideas? 我不知道为什么会这样-有人有任何想法吗? I assume I will have the same error on the line 我想我会在网上有同样的错误

y: yPos(getRandomYPosition),

for the same reason. 出于同样的原因。

I know that xPos is not a function- it is an array, and I am simply trying to retrieve the array element at position 'getRandomXPosition'. 我知道xPos不是一个函数-它是一个数组,我只是试图在位置“ getRandomXPosition”处检索数组元素。

I thought that this might be because 'getRandomXPosition' is not an int itself, it is a function, so I tried storing its output in a variable by changing those function definition lines to: 我认为这可能是因为'getRandomXPosition'本身不是int而是函数,所以我尝试通过将这些函数定义行更改为以下内容来将其输出存储在变量中:

var randomXPosition = function getRandomXPosition(minX, maxX){
    return Math.floor(Math.random()*(maxX - minX +1)) +minX;
}
var randomYPosition = function getRandomYPosition(minY, maxY){
    return Math.floor(Math.random()*(maxY - minY +1)) +minY;
}

and then updating where I was using them so that I was now passing the variables as parameters instead of the functions: 然后更新我使用它们的位置,以便现在将变量作为参数而不是函数传递:

x: xPos(randomXPosition),
y: yPos(randomYPosition),
draggable: true

However, when viewing the page in the browser, I am still getting the console error that says that "xPos is not a function" on the line 但是,当在浏览器中查看页面时,我仍然收到控制台错误,提示“ xPos不是函数”

x: xPos(randomXPosition),

I can't figure out why this is- can anyone point me in the right direction? 我不知道为什么会这样-有人能指出我正确的方向吗? It's probably also worth mentioning that I'm using the kineticJS library to make the images 'draggable' when they're drawn to the canvas- just to give a more complete picture of my code. 可能还值得一提的是,我正在使用dynamicJS库将图像绘制到画布上时使其“可拖动”-只是为了提供我的代码的完整图片。

Any advice would be much appreciated! 任何建议将不胜感激!

Edited 28/01/2013 @ 18:05 编辑于28/01/2013 @ 18:05

Ok, I think I know why the images are all being drawn in the top left corner- the drawImage function that is being called is the one from the KineticJS library, not my own one. 好的,我想我知道为什么所有图像都在左上角绘制-被调用的drawImage函数是KineticJS库中的那个,而不是我自己的。 I am using a copy of the library that I've saved locally, as there are a few things that I have changed regarding the functionality that the library provides. 我使用的是我在本地保存的库的副本,因为有关库提供的功能,我做了一些更改。 Would it make sense to copy the code creating the arrays of positions and selecting the random elements from those positions into the drawImage function in the library instead? 将创建位置数组并从这些位置中选择随机元素的代码复制到库中的drawImage函数中是否有意义?

Edited 29/01/2013 @ 23:15 编辑29/01/2013 @ 23:15

Right, I've had a look at the drawImage function in the kineticJS library (I'm using a local copy of the library), and it looks like this: 是的,我看了dynamicJS库中的drawImage函数(我正在使用该库的本地副本),它看起来像这样:

drawImage: function() {
        var context = arguments[0];
        context.save();
        var a = Array.prototype.slice.call(arguments);

        if(a.length === 6 || a.length === 10) {
            if(a.length === 6) {
                context.drawImage(a[1], a[2], a[3], a[4], a[5]);
            }
            else {
                context.drawImage(a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], a[9]);
            }
        }

        context.restore();
    }

I'm not sure that I fully understand all of the code here... What is the line 我不确定我是否完全理解这里的所有代码。

var a = Array.prototype.slice.call(arguments);

doing? 在做什么? I've not seen this 'slice' function before... 我之前没有看过这个“切片”功能。

Can anyone point out how I would edit this function to include the code I've written to enable the images all to be drawn in separate locations by selecting the coordinates randomly from the arrays of coordinates? 谁能指出我将如何编辑此函数以包括我编写的代码,以通过从坐标数组中随机选择坐标来使图像全部绘制在单独的位置? Presumably, I should just be able to copy and paste this code into the function, but I'm not sure where in the function I should put it... any suggestions? 大概我应该能够将此代码复制并粘贴到该函数中,但是我不确定该在函数中的哪个位置放置...有什么建议吗?

xPos is an array , so you need to use array format to get its element , like xPos[key] , and randomXPosition is a function , you need to execute it to get its return value , like randomXPosition() . xPos是一个数组,因此您需要使用数组格式来获取其元素,例如xPos [key],而randomXPosition是一个函数,您需要执行它以获取其返回值,例如randomXPosition()。

In conclusion , 结论 ,

X: xPos[randomXPosition()],

Well, to access a certain index in the array you have to use [] not (), like so: 好吧,要访问数组中的某个索引,您必须使用[]而不是(),如下所示:

  xPos[randomXPosition] // if randomXPosition = 1, then this gives you the value at xPos[1]. 

  xPos(randomXPosition) // <-- this is a function call, what it is expecting is something like:
  function xPos(number){
     var value = number * Math.random();
     return value; //return some value maybe?
  };

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

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