Solved

HTML5 + Mulitple Dragable Images

Posted on 2013-01-02
8
207 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
 

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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 your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now