Solved

HTML5 + Mulitple Dragable Images

Posted on 2013-01-02
8
211 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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 

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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Suggested Solutions

Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

830 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