Jump to content

paulmo

Members
  • Posts

    451
  • Joined

  • Last visited

Posts posted by paulmo

  1. Hi JSG, thanks...purely client side with localStorage. I'm leaning toward finishing this in canvas, but for comparison, appendChild element script is nicely stacking images next to each other, whereas canvas is just drawing them on top of each other on each form submission...there must be a way to achieve "tiling" images with canvas.

    Here's an example that I modeled existing code from, I'm just integrating a form with it, but obviously not getting the tiled images as they're overlapping. And I'll need to have captions that don't take up their own tileWidth, but rather just go under the image. Thanks for help and direction.

  2. Moving along on this project to make a tiled image gallery like this (w3 example) using url/text form inputs to populate the page with images and captions, I've got 2 scripts going, one using canvas, and the other with appendChild. The appendChild script adds all previous form-submitted images (good), but I need

    1. images same dimension
    2. captions under the images
    3. localStorage integration to access image/caption values to remove, move, etc.

    The canvas script is not adding previous form-submitted images (bad), but it is working with current image. I've commented each script with other things going on (drawImage problem, etc.)

    Thanks in advance for help. I've got appendChild example live here, and canvas example live here (GitHub Pages). I'm posting the entire appendChild below, followed by canvas example.

    <!DOCTYPE html>
    <html>
    <head>
    <title>appendChild Gallery</title>
    
    <style>
    body {
    font-family: Sans-Serif;
    font-size: medium;
    }
    </style>
    
    </head>
    
    <body>
    
    <form action="#">
    
    URL:
    <input type="text" name="image" id="idImage" value="" onClick="this.select();"/>
    <br><br>
    Description:
    <input type="text" name="des" id="idDes" value="" onClick="this.select();"/>
    <br><br>
    <input type="button" id="btn" name="submit" value="Load" />
    </form>
    
    <script>
    	document.getElementById('btn').onclick = function() {
    		var val = document.getElementById('idImage').value;
    		var valDes = document.getElementById('idDes').value;
    		
             	src = val,
    		img = document.createElement('img');
    		img.src = src; // new image
    		document.body.appendChild(img);
    		
    		//previous captions are not being loaded with previous image...why?
    		document.getElementById('caption').innerHTML = document.getElementById('idDes').value;
    /* need localStorage to access values for later.
    		localStorage.setImage('idDes', valDes);
    		localStorage.setImage("idImage", val);
    		
    		imgStore = localStorage.getImage("idImage");
    		desStore = localStorage.getImage("idDes");
    		img1 = document.createElement('img1');
    		img1.src = imgStore; //stored image
    		//desStore.src = desStore;
    		document.body.appendChild(img1);
    		document.getElementById('caption2').innerHTML = document.getElementById('desStore');
    */
            }
    </script>
    
    <!-- Need captions BELOW image -->
    <h4 id = 'caption'></h4>
    
    </body>
    
    </html>

    Canvas example: 

    <!DOCTYPE html>
    <html>
    <head>
    <title>canvasGallery</title>
    
    <style>
    body {
    font-family: Sans-Serif;
    font-size: medium;
    }
    </style>
    
    </head>
    
    <body>
    </br>
    </br>
    
    <form action="#">
    
    Link:
    <input type="text" name="item" id="idImg" value="" onClick="this.select();"/>
    <br><br>
    Descrip.:
    <input type="text" name="des" id="idDes" value="" onClick="this.select();"/>
    <br><br>
    <input type="button" id="btn" name="submit" value="Load" onClick="sendVal(this.form)"/>
    
    <br><br>
    
    </form>
    
    <canvas id="canvas" width="850" height="650"></canvas>
    
    <script>
    function sendVal(form) {
    	
    	imgLink = document.getElementById("idImg").value;
    	desLink = document.getElementById("idDes").value;
    	
    	//how to integrate localStorage so each previous form-submit URL image/caption gets tiled on page? 
    	localStorage.setItem("idImg", imgLink);	
    	localStorage.setItem("idDes", desLink);
    	imgStore = localStorage.getItem("idImg");
    	desStore = localStorage.getItem("idDes");
    	
    	draw();
    }
    function draw(){
        var canvas = document.getElementById('canvas');
        var ctx = canvas.getContext('2d');
        var tileWidth = 100; //
        var tileWCount = 5;
        var multi = 2;
        var imgA = [];
        var src = [];
    	ctx.font = "20px sans-serif"
    	ctx.fillStyle = "#000000";
      
    	/* src.push(ctx.drawImage(imgStore, 200, 10, 70,70)); 
    	
           ^ ^ Uncaught TypeError: Failed to execute 'drawImage' on 'CanvasRenderingContext2D': and window.onload = draw (below) doesn't help.*/
    	
      	src.push(imgLink); //works
    	
    	src.push(ctx.fillText(desLink, 5, 250)); // need each caption under its image, without taking up tileWidth space.
      
        for(var i=0; i < tileWCount; i++){
            var img = new Image();
            img.onload = (function(value){
                return function() {
                    ctx.drawImage(this, (value * tileWidth * multi), 0, tileWidth * multi, tileWidth * multi);
                }
            })(i);
            img.src = src[i];
        }
    }
      
      window.onload = draw; // not helping with drawImage problem.
    </script>
    
    </body>
    
    </html>

     

  3. 2 hours ago, dsonesuk said:

     then in draw() retrieve that value, then push that value into src array

    Retrieving value in draw() seems to be the problem now: imgLink net::ERR_FILE_NOT_FOUND

    <input type="button" id="btn" name="submit" value="Load" onClick="sendVal(this.form);"/>
    </form>
    
    <canvas id="canvas" width="650" height="650"></canvas>
    
    <script>
    
    var imgLink = document.getElementById('idItem').value; 
    
    function sendVal(form) {
        
    function draw(){
    
        var canvas = document.getElementById('canvas');
        var ctx = canvas.getContext('2d');
        var tileWidth = 100;
        var tileWCount = 3;
        var multi = 2;
        var imgA = [];
        var src = [];
        src.push('imgLink');
        
      ....</script>

     

  4. An extension from 1st post (not seeing an edit button )....the following works with static URL in the draw() function, but it's not working with idItem form value into the draw() function, which is what I want. Help appreciated, thanks.

    <form action="#">
    
    URL:
    <input type="text" name="item" id="idItem" value="" onClick="this.select();"/>
    <br><br>
    <input type="button" id="btn" name="submit" value="Load" onClick="sendVal(this.form)";/>
    </form>
    
    <canvas id="canvas" width="650" height="650"></canvas>
    
    <script>
    
    function sendVal(form) {
        document.getElementById('idItem').value;
    }
    
    function draw(){
      
        var canvas = document.getElementById('canvas');
        var ctx = canvas.getContext('2d');
        var tileWidth = 100;
        var tileWCount = 3;
        var multi = 2;
        var imgA = [];
        var src = [];
        
        src.push(sendVal());  //not working
      
        src.push("http://hanes.scene7.com/is/image/Hanesbrands/HNS_HO5944_CamouflageGreenHeather?defaultImage=Hanesbrands/HNS_HO5944_AwesomeBlueHeather&id=fudST1&wid=433&hei=549&fmt=jpg"); //working
      //this works
      
    etc....
    </script>

     

  5. I'd like to make an image gallery like this (w3 example) using url/text form inputs like this (my demo) to populate the page with images and captions, using localStorage or something so that with each form submission the images remain. Can this be done just with static divs and <a> link tags like the w3 example, or would I be creating elements dynamically, or using canvas? Thanks in advance for roadmap on how to get there. 

  6. Posted example as requested so you could see it...code is pretty much there except for additional for loop (view source). You're right I haven't checked to see if objects have been hit already. If you try it online you'll see that for loops tell where box object is, and if it goes to a coordinate, another object is rendered, etc. Just Googling around, state tracking seems a big topic...how/where does would that apply here? Thanks.

  7. Here's an example. Please emulate touch in Chrome console and move box upward to see what's going on. (Device setting: Samsung Galaxy S, SII.) I want "two" to disappear for good when "box" hits "two." After box crosses new x coordinate defined in 2nd for loop (move left), "three" is generated. When box hits "three", I want "three" to disappear for good, etc. Thanks.

  8. I don't use i inside the loop because I'm just checking to see where box object is (that's what i is doing, box object is the one moved by virtual controller), then if box is in that range, create object "two" and so on. The problem is that when box goes out of the range defined in for loop, newly created object "two" disappears and color reverts to old color. So I need the changes to stick and not revert back. How to do that?

  9. Everything works below, except the 2nd spite (TWO) re-appears when BOX moves outside of the position defined in for loop below. Also, BOX reverts to its old color when outside of position in for loop. I need TWO to disappear for the entire game, and BOX to retain new color, after BOX arrives at TWO's position. Thanks for help.

                    for(i = box.y; i < 69; i++) { // Check BOX position and render "TWO"                     context.save();                    context.fillStyle = "#f0ffff";                    context.translate(cx, cy);                    context.rotate(deg * Math.PI/180);                    context.fillRect(two.x, two.y, TWO_SIZE, TWO_SIZE);                    context.restore();            }                for(i = box.y,  j = box.x; i < -115 && j  > 155; i++, j++) {                    console.log("Yes"); // check that BOX is in TWO's position                                context.clearRect(0, 0, canvas.width, canvas.height); // Make TWO disappear                 context.save();                context.fillStyle = "#f0bbbb"; // change BOX color                context.translate(cx, cy);                context.rotate(deg * Math.PI/180);                context.fillRect(box.x, box.y, BOX_SIZE, BOX_SIZE);                context.restore();            }            console.log("X = " + box.x);            console.log("Y = " + box.y);
  10. What is y / 2? Do you mean half of the viewport's height?

    Yes, if Box sprite's y position > 0 then a 2nd sprite will appear at a different position. I'll work on testing sprite position on each game loop, and putting new sprites in an array. I'm sure I'll be back here later. Thanks Ingolme.

  11. Re-visiting this topic. I've rotated the square. Initial position of rotated square is to the left of joystick. I want another square (square 2) to appear in upper right quadrant of screen, if square 1 crosses y/2 (y > 0). How to do this? I'm attaching code for reference. Thanks.

    // BOX OBJECT, x y sets initial star position            var box = {                x : 330,                y : 170,                vx : 0,                vy : 0            };                        // rotating            var cx = BOX_SIZE/2;            var cy = BOX_SIZE/2;            var x = -10;            var y = -10;            var w = 30;            var h = 30;            var deg = 45;            // The game loop            function gameLoop() {                setTimeout(gameLoop, 33);                /* Check inputs */                // Vartical inputs                if(inputBuffer.up) {                    // Move up                    box.vy -= ACCELERATION;                } else if(inputBuffer.down) {                    // Move down                    box.vy += ACCELERATION;                } else {                    // Vertical friction                    box.vy *= INV_FRICTION;                    if(box.vy < MIN_SPEED && box.vy > -MIN_SPEED) {                        box.vy = 0;                    }                }                // Horizontal inputs                if(inputBuffer.left) {                    // Move left                    box.vx -= ACCELERATION;                } else if(inputBuffer.right) {                    // Move right                    box.vx += ACCELERATION;                } else {                    // Horizontal friction                    box.vx *= INV_FRICTION;                    if(box.vx < MIN_SPEED && box.vx > -MIN_SPEED) {                        box.vx = 0;                    }                }                /* Prevent the box from going too fast */                if(box.vx > MAX_SPEED) {                    box.vx = MAX_SPEED;                }                if(box.vx < -MAX_SPEED) {                    box.vx = -MAX_SPEED;                }                if(box.vy > MAX_SPEED) {                    box.vy = MAX_SPEED;                }                if(box.vy < -MAX_SPEED) {                    box.vy = -MAX_SPEED;                }                /* Update the box's position */                box.x += box.vx;                box.y += box.vy;                                /* Draw everthing */                // Clear the screen                context.clearRect(0, 0, canvas.width, canvas.height);                            // BOX OBJECT DRAW                context.save();                context.fillStyle = "#f0ffff";                context.translate(cx, cy);                 context.rotate(deg * Math.PI/180);                context.fillRect(box.x, box.y, BOX_SIZE, BOX_SIZE);                context.restore();    }            // Initialize the controller            GameController.init({                right: { // WAS LEFT                    type: 'joystick',                    joystick : {                        touchMove : function(data) {                            // Horizontal controls                            if(data.normalizedX < -JOYSTICK_THRESHOLD) {                                // Left                               inputBuffer.left = true;                               inputBuffer.right = false;                            } else if(data.normalizedX > JOYSTICK_THRESHOLD) {                                // Right                               inputBuffer.right = true;                               inputBuffer.left = false;                            } else {                                // Neutral                                inputBuffer.left = false;                                inputBuffer.right = false;                            }                            // Vertical controls                            if(data.normalizedY < -JOYSTICK_THRESHOLD) {                               inputBuffer.down = true;                               inputBuffer.up = false;                            } else if(data.normalizedY > JOYSTICK_THRESHOLD) {                               inputBuffer.up = true;                               inputBuffer.down = false;                            } else {                               inputBuffer.up = false;                               inputBuffer.down = false;                            }                        }                    }                }            });            // Begin the game            gameLoop();
  12. Sorry I missed that setTimeout takes a function. Here is complete is_equal function, I'm trying is_equal as function in setTimeout:
    if(is_equal(tiles, solution)) {                                        currentRound++;                                                      if(currentRound < images.length) {                        // Load the next round                       // alert("Next round!");                        selectRound(currentRound);                    } else {                        // The game has ended                        alert("Game over.");                    }                }            }//setTimeout                window.setTimeout(is_equal, 2000);
  13. How to delay the complete round (show complete puzzle for a couple seconds) before loading new round? Thanks.
    // If the round is complete, load the next round                if(is_equal(tiles, solution)) {                    currentRound++;                    if(currentRound < images.length) {                        // Load the next round                       // alert("Next round!");                        selectRound(currentRound);
×
×
  • Create New...