Jump to content
  • Announcements

    • boen_robot

      Guidelines and Netiquette   03/28/2017

      Posting Problems:   Having problems posting your topic? Read through this: To join, you agree to our terms and conditions and fill out and submit a registration form. An activation email will be sent to your email adress, so you'll need to verify your account. After that the account has to be validated by one of the moderators. This will mean that it can take up to a day to be activated. A couple of things to remember to ensure approval: Don't use an email address in one of those $2 four character .com domains eg. xyds.com. These will be deleted and the domain added to the banned list. Don't use an email address that is within a domain with a bad reputation for spam. A Google search is run on every email address and email domain. Don't sign up with an email address that doesn't exist, doesn't work or requires the sender to answer a quiz before their email can get to you. Put your country and or state and city in the signup form. Blank forms will go to the botton of the "to do" list. And make sure that your email address and your country match, saying you're from Alabama and using a .ru email address is not going to get you activated. After a membership is activated the first few posts will be monitored. Posting spam or unapproved topics described in the agreement results in an immediate ban. The email provider and the IP addresses associated with the account will be banned and all posts will be deleted. These strict measures have been deemed necessary to hinder spam. Sorry for any inconvenience this causes, but it's not liable to change. If, after reading this, you still can't post and don't understand why, contact one of the Moderators listed here.   Topic Guidelines   Including the following information can expedite an accurate response from board members: Must be a Specific Problem or Question related to web design and development Include Code in Question (wrap with   for small blocks of code and for longer blocks   ) Include Code Author Include Extra Notes/Modifications/Attempts Include web link to page/file when possible Content Guidelines   You may not post, upload, link to, or email any Content that contains, promotes, gives instruction about, or provides prohibited Content. Prohibited Content includes any Content that breaks any local, state, county, national or international law. Prohibited Content also includes: No direct or indirect advertising or websites, forums, products, services No hijacking of posts (do not post your question in someone elses) Content that infringes upon any rights [ex. MP3s and ROMs] (including, but not limited to, copyrights and trademarks) Abusive, threatening, defamatory, racist, or obscene Viruses or any other harmful computer software False Information or libel Spam, chain letters, or Pyramid schemes Gambling or Illicit drugs Terrorism Hacking or cheating for internet/online games Warez, Roms, CD-Keys, Cracks, Passwords, or Serial Numbers Pornography, nudity, or sexual material of any kind Excessive profanity Invasive of privacy or impersonation of any person/entity Hacking materials or information Posting Tips   There are more BBcodes than there are buttons for on the reply menu. To get the full list, click "BB Code Help" underneath the clickable smilie face menu. Use   for small snippets of code Use   for lengthy snippets of code Use   if your snippet is HTML (optional) Use   if your snippet is SQL (optional) Rules of Conduct   Be nice. There's no need for calling someone stupid if they ask an 'easy' question. Keep your avatars and signatures absolutely child friendly. We have a younger audience on this forum. Keep your language appropriate for the same reason above. Do not PM moderators for help on the forum. Post on the topic, or create a new one.   Spam:   Recently, as you have all without doubt noticed, we have had lots of spam and advertisement on the forum. Therefore, we'd like to alert you as to what to do when you have found any of the aforementioned annoying messages: it. Immediately. Give a clear reason, please, if the advertising is not evident. DO NOT POST! Report, let the post stay as is, and we will get to it, meanwhile if you continue to post as normal in the other threads, it won't be on the top so long. Refrain from PMing the member. This won't help at all, as they are most likely spambots anyway. Thank you.       Images in signatures:   After thinking of users on dial-up, we have decided to enforce the following rules regarding signatures. Please pay heed to them. Respecting these rules is respecting the members on this forum with dial-up. Signature rules: No animated images AT ALL. No matter the amount of animation. Maximum image widthxheight: 300x150 Maximum image (file) size: 15kb Use calm colors. Do not use highly contrasting images in your signature, as this can get really annoying when seeing several posts from one member in the same thread. The same prohibited content goes for images as for posts. Lastly, use common sense. No lengthy signatures please. Save us some scrolling. Thank you.       Links in signatures:   Please understand that w3schools.com only exists because of voluntary work and is barely supported by the advertising littered throughout the tutorials and the forum. So, please, stop advertising other sites. DO not post links that drive traffic away from the w3schools domain - especially to a site that offers similar if not identical information. Please help support the site by keeping individuals on it. Thank you. Here are some guidelines as to what you can put in your signature: w3schools links --> allowed w3.org links --> allowed browser links --> allowed html editor links --> allowed personal sites --> allowed tutorial sites competing with w3schools --> NOT allowed sites completely irrelevant to webprogramming and this forum --> NOT allowed   Thanks for understanding, and for taking the time to read this. ~W3Schools Modstaff~
paulmo

image/caption gallery page from url/text form inputs

Recommended Posts

paulmo    3

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. 

Share this post


Link to post
Share on other sites
paulmo    3

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>

 

Share this post


Link to post
Share on other sites
dsonesuk    702

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.

Share this post


Link to post
Share on other sites
paulmo    3

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? 

Share this post


Link to post
Share on other sites
dsonesuk    702

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.

Share this post


Link to post
Share on other sites
paulmo    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>

 

Edited by paulmo

Share this post


Link to post
Share on other sites
dsonesuk    702

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.

Share this post


Link to post
Share on other sites
paulmo    3

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

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

 

Share this post


Link to post
Share on other sites
paulmo    3

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

Share this post


Link to post
Share on other sites
justsomeguy    941

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?

Share this post


Link to post
Share on other sites
paulmo    3

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.

Share this post


Link to post
Share on other sites
justsomeguy    941
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

Share this post


Link to post
Share on other sites
paulmo    3

Thanks for clarifying...the DOM it is then. I'll work on that and come back when I get stuck. 

Share this post


Link to post
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

×