Jump to content

image/caption gallery page from url/text form inputs


paulmo

Recommended Posts

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. 

Link to comment
Share on other sites

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>

 

Link to comment
Share on other sites

This does nothing,

document.getElementById('idItem').value;

but describes a element with id 'idItem' with attribute value, OK! document.getElementById('idItem') element may or may not have a value, what do you want to actually do with it?, it should be applied to a variable, and what I think you want is to return that variable value. I say 'think because this function is used on a inline onclick event, which requires a form parameter which

src.push(sendVal());

Does Not provide.

Link to comment
Share on other sites

Hi, as mentioned in my first post, the code works with static URL in the draw() function. I need the form-submitted idItem URL value to be "sent" to src.push and work the same as the static URL does. Can you help with that? 

Link to comment
Share on other sites

As I already said, You need to apply the value of document.getElementById('idItem').value; to a variable, THEN call the draw() function from within sendVal() function with that value as an parameter, then in draw() retrieve that value, then push that value into src array (I should rename this so that to conflict with src attribute). BUT! you also need to prevent form from submitting using .preventDefault(), else it will submit and clear everything.

Link to comment
Share on other sites

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>

 

Edited by paulmo
Link to comment
Share on other sites

I said 'THEN call the draw() function from within sendVal()' NOT place draw function within sendVall()

onClick="sendVal(this.form)"

calls the function sendVal() function with parameters for a specific form that is triggered on the onclick event.

src.push('imgLink');

By wrapping the variable imgLink in single quotes, it is pushing in the array just 'imgLink' a text string.

Link to comment
Share on other sites

Thanks so much! Indeed, calling draw() from within sendVal() and src.push(imgLink) does it.

function sendVal(form) {
    imgLink = document.getElementById('idItem').value;
    draw();
}

 

Link to comment
Share on other sites

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>

 

Edited by paulmo
Link to comment
Share on other sites

Do you want this to be purely client-side, so that every browser on every device sees their own version, or do you want everyone to see the same images?  Also, why do you want to use a canvas as opposed to just creating the element structure from the example you linked to?

Link to comment
Share on other sites

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.

Edited by paulmo
discovered that canvas is overlapping images, not overwriting them.
Link to comment
Share on other sites

4 hours ago, paulmo said:

there must be a way to achieve "tiling" images with canvas.

Sure, you can do all of the layout math yourself that the browser would normally do when it positions elements.  You can decide how large you want each thing to be, how much spacing between them, how large the canvas is, etc, and figure out the coordinates to draw everything.  If you want to lay out everything yourself on a canvas then you can do that, you just need to do all of the math that the browser would normally do for you.  If you want a border around the image and caption, for example, then you need to decide how thick the border is and account for that space, any space between the border and the inner contents, etc.  The same thing goes for text, if you want the text to wrap to the next line then it's your responsibility to figure out how many pixels long the text is and where to wrap it, there's a description about that here.  When you draw on a canvas you're not using CSS to position things on the canvas and then having the browser lay it all out, you're doing all of that manually and just telling the browser the coordinates to draw everything.

 

Anyway, that's why I was wondering why you're using a canvas instead of just creating the div and img tags and adding them to the document, where the browser will handle layout, position, wrapping, etc.

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...