How to build a Preview image by stacking transparent images based on Form selections using JavaScript?

css, html, javascript, jquery

Solution

I created an Angular app that does this, in a more flexible way.

FIDDLE: http://jsfiddle.net/Pha4V/3/

You can easily add shapes, types and colors just by creating new elements in an array. Also, by having the structure defined this way, the code can be easily integrated with a database.

Example:

var tops =
[
    new Shape("Triangle", "https://i.stack.imgur.com/wkRaA.png", "Blue"),
    new Shape("Triangle", "https://i.stack.imgur.com/lHk5X.png", "Green"),
    new Shape("Triangle", "https://i.stack.imgur.com/LT6Nn.png", "Red"),
    new Shape("Octagonal", "https://i.stack.imgur.com/FjF7S.png", "Red"),
    new Shape("Octagonal", "https://i.stack.imgur.com/0FWGv.png", "Blue"),
    new Shape("Octagonal", "https://i.stack.imgur.com/92kyY.png", "Green")
];

For the future, fonts can be added by adding a fourth parameter and new colors can be added just by adding an element with that color.

New color example:

...
    new Shape("Octagonal", "http://...", "Cyan")
...

New shape type example:

...
    new Shape("Square", "http://...", "Blue")
...

The current form of the 'house':

It could be easily saved, as there are variables that define all the selections:

$scope.topColor, $scope.topType
$scope.bottomColor, $scope.bottomType
...

Different methods of input:

The select method can be changed easily, as the code does not depend on it's type.

Future fonts:

You can easily add new shapes as fonts, and copy the logic from 'tops' and you won't have to generate that many images. You'll have just to generate images with different fonts:

var fontBottom =
[
    new Shape("Font", "http://...", "Arial"),
    new Shape("Font", "http://...", "Arial Black")
];

//Then add the methods and the input.

Problem

Using HTML, CSS, and JavaScript I need to build a Form with Selection input fields and Radio selection fields. When each one of these fields is selected, I need to build a Preview image based on the selection they made. Each field will have an Image associated with it and then it will also have that same image replicated for each color. So if there was 20 Form fields that a user could pick it would have 20 images x 10 different color options and would result in there being 200 possible images to build there preview image! Here is an image that shows an example of what I am trying to accomplish...the Vans website uses this technique for a custom shoe generator with preview... When you pick a section and then the color for that section, it loads a transparent image and stacks them together. For testing, I have made a set of transparent images for a demo form with only 4 Fields and 3 color choices so 12 images in total. (All shown below) When all the fields have been selected it should build an image preview to the user that is similar to this... or the Alternative has a Top/Roof more like an octagon type shape bottom_blue bottom_green bottom_red entrance_blue entrance_green entrance_red top_blue top_green top_red top_red_octo top_blue_octo top_green_octo Here is a basic form that could use the images... http://jsfiddle.net/Pha4V/ ``` <form id="form-ChannelType"> <p> Select a bottom (just 1 in the demo)<br> <select name="bottom" class="form-control" id="bottom" size="1"> <option value="bottom1">Bottom 1</option> </select> </p> <p> Select a Front (just 1 in the demo)<br> <select name="front" class="form-control" id="front-1" size="1"> <option value="front1">Front 1</option> </select> </p> <p> Select a Top<br> <select name="bottom" class="form-control" id="bottom" size="2"> <option value="bottom_triangle">Triangle Shaped Top</option> <option value="bottom_octo">Octo. Shaped Top</option> </select> </p> <p> Select a Color<br> <select name="color" class="form-control" id="color" size="3"> <option value="blue">Blue</option> <option value="green">Green</option> <option value="red">Red</option> </select> </p> </form> ``` Can anyone help me in the logic of how to make such a form work? I need to make it work in a way that can easily scale to hundreds of options and images. With the basic form above, it would need to build a preview image by stacking appropriate images based on the form selections. I don't expect anyone to do all the work for this unless they are simply up for the challenge but I would appreciate any help or ideas on how to best make this work? NOTE: My final project that will be based off of this will be much different. It will be used for a custom sign generator. I cannot use ImageMagick or GD libraries to do on the fly images, so each image has to be built and each layer changed. Instead of abuilding a simple house structure I will have options such as... - Sign Type which will determine a lot of the setting that are shown or hidden after it based on the type of sign selected. - Font the base image will have one for each font and in each font will be in each color as well. So 10 fonts x 15 colors = could easily be over 150 possible images to show just as the BASE image - Colors There will be from 2-4 different color options that will change the color for different parts of the sign. So all the image combinations listed above x the different color areas = lots of images So you can see this will be a pretty big beast when it's all done and I could use any help in getting a good start on it. I don't think a HUGE if/else or switch statement is the best way to go with something like this that can have hundreds of image combinations but I could be wrong?

Original source