Solved

HTML5 + Mulitple Dragable Images

Posted on 2013-01-02
8
210 Views
Last Modified: 2013-01-03
I am using the below code to create a dragable image, i want to be able to create multiple individual dragable versions of the image depending on a variable, each image independently dragable onto the conatiner.  Is this possible if so how?


<html>
  <head>
    <style>
      body {
        margin: 5px;
        padding: 5px;
      }
        #container {
        background-color: #336600;
        width: 400px;
        height: 600px;
    </style>
    <script src="http://d3lp1msu2r81bx.cloudfront.net/kjs/js/lib/v4.2.0/kinetic-v4.2.0.js"></script>
  </head>
   <body>
    <div id="container"></div>
    <script src="http://www.html5canvastutorials.com/libraries/kinetic-v4.2.0.js"></script>
    <script>
        function drawImage(imageObj) {
            var stage = new Kinetic.Stage({
                container: "container",
                width: 500,
                height: 600
            });
            var layer = new Kinetic.Layer();

            // darth vader
            var PlayerImg = new Kinetic.Image({
                image: imageObj,
                x: 445, //stage.getWidth() / 2 - 200 / 2,
                y: 5, // stage.getHeight() / 2 - 137 / 2,
                width: 50,
                height: 75,
                draggable: true
            });

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

            layer.add(PlayerImg);
            stage.add(layer);
        }
        var imageObj = new Image();
        imageObj.onload = function() {
            drawImage(this);
        };
        imageObj.src = 'images/Player.JPG';

    </script>
  </body>
</html>
0
Comment
Question by:dkilby
  • 4
  • 4
8 Comments
 
LVL 17

Expert Comment

by:jrm213jrm213
ID: 38740009
Hi,

It definitely sounds like you can do that with kinetic. I have noticed working with the kinetic library though that you have to do things in a way that may be a little counter-intuitive, but they have great examples of how to do a lot of things on their website.

You should just need to add a loop to that script. And move the stage and layer items out of the function and make them global to the page (you don't want separate stages for each image, though I guess it is possible you want multiple layers)

So you create the stage and the layer, then you loop the creation of  your images and make sure to draw the layer each time you add something to it, unless they changed something, it didn't used to automatically redraw.

new code
<script>
var stage = null;
var layer = null;
function drawImage(imageObj) {         
            // darth vader
            var PlayerImg = new Kinetic.Image({
                image: imageObj,
                x: 445, //stage.getWidth() / 2 - 200 / 2,
                y: 5, // stage.getHeight() / 2 - 137 / 2,
                width: 50,
                height: 75,
                draggable: true
            });

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

            layer.add(PlayerImg);
            layer.draw();
        }

window.onload = function()
{
         var x = 5; //you could set this to whatever number of images you want
         stage = new Kinetic.Stage({
                container: "container",
                width: 500,
                height: 600
            });
           layer = new Kinetic.Layer();
           stage.add(layer);

        

	var imageObjs = Array();
	for(var i = 0;i<x;i++)
	{
		imageObjs.push(new Image());
		imageObjs[i].onload= function() {
		    drawImage(this);
		};
		imageObjs[i].src = 'images/Player.JPG';
	}
	
}
 
</script>

Open in new window


I tested the above and it works. I am running the stage creation on  the window.load even so that we ensure that the html element with id of "container" is loaded.
0
 

Author Comment

by:dkilby
ID: 38741041
is there a way to have the images line up vertically, so they are not all on top of each other?  so the first one is position y of 5, the next one when added would be y + the height of theimage.

thanks
0
 
LVL 17

Accepted Solution

by:
jrm213jrm213 earned 500 total points
ID: 38741168
well, you could update the drawImage function to accept an x and y parameter and then pass them to the function when you draw them...

you could determine the ypos by giving the index of the loop as the alt tag of the image and then calculate in the onload function.

<script>
var stage = null;
var layer = null;
var i = 0;
function drawImage(imageObj,xpos,ypos) {         
            // darth vader
            var PlayerImg = new Kinetic.Image({
                image: imageObj,
                x: xpos, //stage.getWidth() / 2 - 200 / 2,
                y: ypos, // stage.getHeight() / 2 - 137 / 2,
                width: 50,
                height: 75,
                draggable: true
            });

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

            layer.add(PlayerImg);
            layer.draw();
        }
window.onload = function()
{
         var x = 5; //you could set this to whatever number of images you want
         stage = new Kinetic.Stage({
                container: "container",
                width: 500,
                height: 600
            });
           layer = new Kinetic.Layer();
            stage.add(layer);

        

	var imageObjs = Array();
	for(i = 0;i<x;i++)
	{
		imageObjs.push(new Image());
		imageObjs[i].alt = i;
		imageObjs[i].onload= function() {
		    drawImage(this,450,5 + this.height * parseInt(this.alt));
		};
		imageObjs[i].src = 'images/Player.JPG';
	}
	
}
 
</script>
                                            

Open in new window

0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:dkilby
ID: 38741948
Only the 1st and last image show up with this code, there is space inbetween that looks about the same distance as the other 3 images, but only 2 show up.
0
 
LVL 17

Expert Comment

by:jrm213jrm213
ID: 38741999
hmm, they all showed up for me...

try adding in stage.draw(); after the for loop.

you can see my example here:

http://bit.ly/UmVSVG
0
 

Author Comment

by:dkilby
ID: 38742020
definitely works for you, i copied your code exactly from your page and only get 2 images.  I stepped through and it seems on my machine this line is creating higher numbers.

drawImage(this,450,5 + this.height * parseInt(this.alt));

the parseInt(this.alt) is 0,1,2,3,4 - so 5 + 75 * 0,1,2,3, or 4 is creating high numbers which then have the other 3 images off the screen
0
 
LVL 17

Expert Comment

by:jrm213jrm213
ID: 38742034
hmm,

ok, so are the images you are using actually 75px tall or are they being scaled when they are drawn by kinetic. The way I wrote the code it gets the actual height of the image. Try swapping out that line with this

drawImage(this,450,5 + 75 * parseInt(this.alt));
0
 

Author Comment

by:dkilby
ID: 38742049
that worked, i didnt realize i had the images bigger than 75px and then scaled down - thanks for the help.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question