jQuery + ColdFusion 8 = Erotic And Magnetic Poetry

Posted August 31, 2007 at 8:08 AM

Tags: ColdFusion, Javascript / DHTML

I just opened a ColdFusion 8 account at HostMySite.com and I'm already having fun playing around with all the new features of ColdFusion 8. Yesterday, I put up a demo that combines the unbelievable simplicity of jQuery with the image manipulation power of ColdFusion 8 to create a drag-and-drop magnetic poetry system; you know, like the kind you see on the refrigerator doors in college dorm rooms and young adult apartments. Using jQuery, the user can add images to the "Canvas". Then, using the a jQuery plugin (Interface), the user can drag and drop the magnetic pieces around on the Canvas. Once the positioning is done, the image data is submitted to the server where ColdFusion 8 will compile all the pieces into one PNG image that is streamed to the browser.


 
 
 

 
jQuery And ColdFusion 8 Erotic, Magnetic Poetry Graphic  
 
 
 

Lots of fun to be had. Try it out for your self here:

jQuery + ColdFusion 8 = Erotic And Magnetic Poetry Online Demo

The system was actually quite simple to build and consists of only two ColdFusion templates and some jQuery files. The hardest part of the system was building the images used for the actual magnetic pieces. One day, I might be able to build the magnetic images using ColdFusion 8, but I doubt the server has my web site font.

The first ColdFusion 8 page displays the canvas and uses jQuery to manipulate the magnetic pieces:

 Launch code in new window » Download code as text file »

  • <!--- Kill extra output. --->
  • <cfsilent>
  •  
  • <!--- Query for word images. --->
  • <cfdirectory
  • action="list"
  • directory="#ExpandPath( './words/' )#"
  • type="file"
  • listinfo="name"
  • sort="name ASC"
  • name="qWord"
  • />
  •  
  • <!---
  • Loop over words to get rid of file extensions.
  • We are going to be using these values to create the
  • word list and we don't need the file extensions
  • (we are going to assume they are all GIF images).
  • --->
  • <cfloop query="qWord">
  •  
  • <!--- Get the word from the file name. --->
  • <cfset qWord[ "name" ][ qWord.CurrentRow ] = ListFirst(
  • qWord.name,
  • "."
  • ) />
  •  
  • </cfloop>
  •  
  • </cfsilent>
  •  
  • <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  • <html>
  • <head>
  • <title>Magnetic Poetry With ColdFusion 8 And jQuery</title>
  •  
  • <style type="text/css">
  •  
  • body,
  • html {
  • background-color: #FFFFFF ;
  • font: 11px verdana ;
  • margin: 0px 0px 0px 0px ;
  • padding: 0px 0px 0px 0px ;
  • }
  •  
  • div#canvas {
  • border: 1px solid #999999 ;
  • height: 600px ;
  • left: 10px ;
  • overflow: hidden ;
  • position: absolute ;
  • top: 10px ;
  • width: 800px ;
  • }
  •  
  • div#wordlist {
  • border: 1px solid #999999 ;
  • height: 600px ;
  • left: 815px ;
  • overflow: auto ;
  • position: absolute ;
  • top: 10px ;
  • width: 200px ;
  • }
  •  
  • div#wordlist a {
  • background-color: #F0F0F0 ;
  • cursor: pointer ;
  • display: block ;
  • line-height: 20px ;
  • margin: 0px 0px 1px 0px ;
  • text-align: center ;
  • }
  •  
  • div#wordlist a:hover {
  • background-color: #FFFFFF ;
  • }
  •  
  • div.magnet {
  • position: absolute ;
  • z-index: 100 ;
  • }
  •  
  • div.magnet img {
  • display: block ;
  • position: relative ;
  • }
  •  
  • form {
  • height: 30px ;
  • left: 10px ;
  • margin: 0px 0px 0px 0px ;
  • position: absolute ;
  • top: 615px ;
  • width: 802px ;
  • }
  •  
  • button {
  • background-color: #FAFAFA ;
  • border: 1px solid #999999 ;
  • display: block ;
  • font-size: 18px ;
  • height: 30px ;
  • margin: 0px auto 0px auto ;
  • width: 800px ;
  • }
  •  
  • textarea {
  • display: none ;
  • }
  •  
  • </style>
  •  
  • <!-- Linked Files. -->
  • <script
  • type="text/javascript"
  • src="./jquery-1.1.4.pack.js">
  • </script>
  •  
  • <script
  • type="text/javascript"
  • src="./interface.drag-drop.js">
  • </script>
  •  
  • <script type="text/javascript">
  •  
  • // Define all the words that are available.
  • var arrWords = (
  • <cfoutput>
  • "#ValueList( qWord.name, "|" )#"
  • </cfoutput>
  • ).split( "|" );
  •  
  •  
  • // When a user clicks on the word link, this initiates
  • // the magnetic adding function (will not fully execute
  • // until the image has loaded).
  • function AddWord( jCanvas, jLink ){
  • var jMagnet = null;
  • var jImg = null;
  •  
  • // Create the magnet DIV.
  • jMagnet = $( "<div>" ).addClass( "magnet" );
  •  
  • // Set the image value.
  • jMagnet.attr( "image", jLink.text() );
  •  
  • // Create the image.
  • jImg = $( "<img>" );
  •  
  • // Set the load function for the img.
  • jImg.load(
  • function(){
  • var jImg = $( this );
  • var jDiv = $( this.parentNode );
  •  
  • // Set the position.
  • jDiv.css( "top", "30px" );
  • jDiv.css( "left", "30px" );
  •  
  • // Set the Div width height.
  • jDiv.width( jImg.width() );
  • jDiv.height( jImg.height() );
  •  
  • // Make the magnet draggable.
  • jDiv.Draggable(
  • {
  • <!--- containment: "parent", --->
  • zIndex: 1000,
  • ghosting: true,
  • opacity: 0.7
  • }
  • );
  • }
  • );
  •  
  • // Append the image to the div.
  • jMagnet.append( jImg );
  •  
  • // Set the image source.
  • jImg.attr(
  • "src",
  • ("words/" + jLink.text() + ".gif")
  • );
  •  
  • // Add the magnet to the canvas.
  • jCanvas.append( jMagnet );
  •  
  • // Return out.
  • return;
  • }
  •  
  •  
  • // This will store the image data into the form and then
  • // submit the form to the page that generates the image
  • // using ColdFusion 8 image functionality.
  • function GenerateImage( jForm, jCanvas ){
  • var jData = $( "textarea#data" );
  • var strData = "";
  • var jMagnet = jCanvas.find( "div.magnet" );
  •  
  • // Loop over the magnets to build the data
  • // string.
  • jMagnet.each(
  • function( intI ){
  • var jDiv = $( this );
  •  
  • // Append list data for image.
  • strData += (
  • jDiv.attr( "image" ) + ":" +
  • parseInt( jDiv.css( "top" ) ) + ":" +
  • parseInt( jDiv.css( "left" ) ) + ","
  • );
  • }
  • );
  •  
  • // Store the data value in the form.
  • jData.val( strData );
  •  
  • // Submit the form.
  • jForm.submit();
  • }
  •  
  •  
  • // This prepares the document once the body has
  • // finished loading and the DOM is ready for
  • // interaction.
  • $(
  • function(){
  • var jCanvas = $( "div#canvas" );
  • var jList = $( "div#wordlist" );
  • var jForm = $( "form" );
  • var jWord = null;
  •  
  • // Loop over all the words.
  • for (
  • var intWord = 0 ;
  • intWord < arrWords.length ;
  • intWord++
  • ){
  •  
  • // Create a link for the word.
  • jWord = $(
  • "<a>" +
  • arrWords[ intWord ] +
  • "</a>"
  • );
  •  
  • // Bind the onclick action.
  • jWord.click(
  • function(){
  • AddWord( jCanvas, $( this ) );
  • }
  • );
  •  
  • // Add the link to the word list.
  • jList.append( jWord );
  • }
  •  
  •  
  • // Set up the form so that we have control
  • // over the submission procress.
  • jForm.find( "button" ).click(
  • function(){
  • GenerateImage( jForm, jCanvas )
  • }
  • );
  • }
  •  
  • );
  •  
  • </script>
  • </head>
  • <body>
  •  
  • <!--- The magnetic canvas. --->
  • <div id="canvas"></div>
  •  
  • <!--- The list of words. --->
  • <div id="wordlist"></div>
  •  
  • <!--- Generate Image form. --->
  • <form action="generate.cfm" method="post" target="_blank">
  •  
  • <button type="button">
  • Generate Image Using ColdFusion 8 &raquo;
  • </button>
  •  
  • <!--- This will store the magnet positional data. --->
  • <textarea name="data" id="data">Blam</textarea>
  •  
  • </form>
  •  
  • </body>
  • </html>

The only ColdFusion 8 specific code on the main page is the fact that the ColdFusion CFDirectory tag is using the ListInfo and Type attributes to make sure that we get only the name values of files in the graphics directory. Other than that, jQuery is really what dominates this page. This works seamlessly on FireFox, but in IE, there is some weird ghosting issues on the drag-and-drop feature. Also, it took me a good amount of time to figure out that ghosting would NOT work if the DIV had an explicit "display: block" style (who knows why?!?).

Once you are done dragging your images around, clicking on the "Generate Image Using ColdFusion 8" button will grab the image data (including positions on the canvas) using jQuery, store it in the Form, and then submit it to another ColdFusion template, generate.cfm. This ColdFusion page then reads in all the images into ColdFusion 8 image objects and pastes them onto a blank canvas. Once all the images are done, I am then just writing the image to the browser.

 Launch code in new window » Download code as text file »

  • <!--- Kill extra output. --->
  • <cfsilent>
  •  
  • <!--- Param form data. --->
  • <cfparam
  • name="FORM.data"
  • type="string"
  • default=""
  • />
  •  
  •  
  • <!--- Create a new canvas. --->
  • <cfset imgCanvas = ImageNew(
  • "",
  • 800,
  • 600,
  • "rgb",
  • "FFFFFF"
  • ) />
  •  
  •  
  • <!---
  • Loop over the form data as a comma
  • delimited list of magnets data-lists.
  • --->
  • <cfloop
  • index="lstMagnetData"
  • list="#FORM.data#"
  • delimiters=",">
  •  
  • <!--- Get the parts of the magnet data. --->
  • <cfset arrParts = ListToArray( lstMagnetData, ":" ) />
  •  
  • <!--- Make sure that we have three parts. --->
  • <cfif (ArrayLen( arrParts ) EQ 3)>
  •  
  • <!--- Read in the word image. --->
  • <cfimage
  • action="read"
  • source="./words/#arrParts[ 1 ]#.gif"
  • name="imgWord"
  • />
  •  
  • <!---
  • Paste the word image into the canvas using the
  • passed in top/left coordinates.
  • --->
  • <cfset ImagePaste(
  • imgCanvas,
  • imgWord,
  • arrParts[ 3 ],
  • arrParts[ 2 ]
  • ) />
  •  
  • </cfif>
  •  
  • </cfloop>
  •  
  •  
  • <!---
  • ASSERT: At this point, we have pasted in all the
  • magnetic words onto the canvas.
  • --->
  •  
  • </cfsilent>
  •  
  •  
  • <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  • <html>
  • <head>
  • <title>jQuery And ColdFusion 8 - Magnetic Poetry</title>
  •  
  • <style type="text/css">
  •  
  • body {
  • background-color: #F0F0F0 ;
  • }
  •  
  • img {
  • border: 1px solid #666666 ;
  • display: block ;
  • margin: 30px auto 0px auto ;
  • }
  •  
  • </style>
  • </head>
  • <body>
  •  
  • <!--- Write the image to the browser. --->
  • <cfimage
  • action="writetobrowser"
  • source="#imgCanvas#"
  • format="png"
  • />
  •  
  • </body>
  • </html>

This whole thing was really easy to put together. I can already see how amazingly powerful all the new features of ColdFusion 8 are going to be. A few months ago, I would have never been able to do something like this. If I even attempted it, I would have had to build a Flash movie or something that somehow exported to an image. It would have been a pain in the butt. Now, with ColdFusion 8, doing this sort of image manipulation took a white-space-heavy 100 lines of code.

ColdFusion 8 is mad sexy!

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Other Searches  |  Print Page



Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

IRz
Aug 31, 2007 at 9:31 AM // reply »
5 Comments

cfyeahbaby, next cforalsex. and soon cfvirtualwomen. go cf8 go.


Aug 31, 2007 at 9:41 AM // reply »
95 Comments

Man, you just got too much time on your hands, he he. Neat example though.


Aug 31, 2007 at 9:48 AM // reply »
188 Comments

Move over Ben Forta, CF8 has a new 'pimp' in town. ;P Anyway, I concur... you have too much time on your hands. :)


Aug 31, 2007 at 9:56 AM // reply »
7,539 Comments

Hey, better in front of a computer than out in a gang, right?


Aug 31, 2007 at 10:21 AM // reply »
78 Comments

Ben...

You should be able to upload your own font and specify that to be used. Great example but...sometimes your mother and I worry about you.

:)


Aug 31, 2007 at 10:28 AM // reply »
7,539 Comments

@Andy,

I don't have that kind of access to the server :( But regardless, I should try to work some kind of magic with whatever font they have, just so I can get the algorithm down.

And don't worry about me - I don't plan to even use a computer this weekend :)


Aug 31, 2007 at 10:47 AM // reply »
78 Comments

You can't even upload a font? With Image.cfc, you simply upload a font to any directory for which you have permissions, then pass in the path to the font. Couldn't be easier.


Aug 31, 2007 at 11:14 AM // reply »
7,539 Comments

@Andy,

I think the font needs to be the system folder? To be honest, I am not sure how exactly the fonts are read in for ColdFusion. If it has anything to do with the install of system files or CF Admin, I don't have access to that stuff.


Aug 31, 2007 at 12:53 PM // reply »
17 Comments

how do you delete a word once you put it up there? heaven forbid you should make a mistake in your composition, but still...


Aug 31, 2007 at 1:00 PM // reply »
7,539 Comments

@Michael,

Good question. I just drag it off the canvas. It is still "there", but it just won't show. I was trying to figure out how to do that. I might add a Key Capture feature (for delete) or maybe make a "Hot Area" for drag and drop delete (ie. Drop over bottom right corner to delete).


ck
Sep 3, 2007 at 4:53 AM // reply »
1 Comments

For fonts you can copy them on the server at any place and if you have access to administrator, you can add them through font management.

The fonts that you add are considered as user fonts and should get preference over system fonts.The only difference I think is that they will not be a part of any fall back algo.


Sep 4, 2007 at 7:24 AM // reply »
7,539 Comments

@CK,

When you say:

copy them on the server at any place

.. What do you mean? Copy them where? Do you mean I can reference the fonts from any folder?


Sep 20, 2007 at 4:52 AM // reply »
1 Comments

By the way, ecstacy is spelt "ecstasy"


Sep 20, 2007 at 7:14 AM // reply »
7,539 Comments

Yeah, spelling is certainly not my strong point :) Thanks for pointing that out.


Jan 9, 2008 at 3:47 AM // reply »
4 Comments

Althought,i can not understand some parts of the article,thanks for sharing.


abi
Apr 23, 2008 at 2:27 PM // reply »
2 Comments

Where to download the interface jquery files needed for this to work?


Apr 23, 2008 at 2:48 PM // reply »
7,539 Comments

@Abi,

Try http://www.jquery.com.


abi
Apr 23, 2008 at 3:16 PM // reply »
2 Comments

I was able to download jquery-1.2.3.min.js from there but this example seems to be using two different js files. (jquery-1.1.4.pack.js, interface.drag-drop.js) & i couldn't find any downloadable links for these two?


Apr 23, 2008 at 3:25 PM // reply »
7,539 Comments

@Abi,

You can just grab the ones from my demo if you want:

http://www.cf8testing.com/demo/magnetic_poetry/jquery-1.1.4.pack.js

http://www.cf8testing.com/demo/magnetic_poetry/interface.drag-drop.js

Enjoy.


Jul 2, 2008 at 3:38 PM // reply »
1 Comments

Do you have a solution for the ghosting issue in IE? As you say, in FF it works great, but for some reason IE seems to be afraid of ghosts... ;)


Jul 2, 2008 at 3:39 PM // reply »
7,539 Comments

@Dave,

I have not found a solution for this.


Jan 2, 2009 at 12:00 AM // reply »
1 Comments

This is nice, have you re-written it for the latest version of jquery?

Adrian


Jun 18, 2009 at 9:19 PM // reply »
1 Comments

I tried watching the live demo, but looks like is not working...do you have another site where we could see this?
Thanks
Carlo


Jun 19, 2009 at 7:02 PM // reply »
7,539 Comments

@Carlo,

Sorry, the CF8 beta account closed a long time ago. You can now see it here:

http://www.bennadel.com/resources/cf8testing/demo/magnetic_poetry/


Mar 16, 2010 at 8:54 AM // reply »
1 Comments

ive demo, but looks like is not working...do you have another site where we could see this?


Mar 17, 2010 at 9:59 AM // reply »
7,539 Comments

@yatakta,

All the CF8 testing stuff has been moved here:

http://www.bennadel.com/resources/cf8testing/


Post Comment  |  Ask Ben

Recent Blog Comments
Mar 18, 2010 at 10:28 PM
Posting XML SOAP Requests With jQuery
can you please point me to the jquery documentation on the following # // Create our SOAP body content based off of # // the template. # var soapBody = soapTemplate.html().replace( # new RegExp( "\\ ... read »
Mar 18, 2010 at 6:34 PM
Exploring ColdFusion Component Runtime Class Properties And Serialization
@Ben Very useful analyses. Thank you @Elliot Thanks for additional clarification Though, it's quite a shame that getBust() failed...not defined ;) ... read »
Mar 18, 2010 at 5:35 PM
Exploring ColdFusion Component Runtime Class Properties And Serialization
Saving private properties is necessary so that you can "reconstitute" an object on the other side of the wire, or load up a serialized object you saved to disk. If it didn't save the private state o ... read »
Mar 18, 2010 at 4:04 PM
jQuery's Event Triggering, Order Of Default Behavior, And triggerHandler()
Tks! You saved-me today. it can be chained into one statement: $("#x).attr("checked","checked").triggerHandler('click'); ... read »
Mar 18, 2010 at 1:18 PM
Finally Finished Ayn Rand's Atlas Shrugged Audio Book
@joaopft, Not disputing what you say - but... If I understand you correctly, you are saying that Positivism is based on sense experience (what I experience is what is), but Quantum theory states tha ... read »
Mar 18, 2010 at 11:48 AM
Duplicate() Much Faster Than ColdFusion Query-of-Queries
I am working on a massive xml parsing, qofq app to create 2 seperate xml files. I just don't understand the concept/purpose of duplicate function, are you duplicating the data or the row, into a new ... read »
Mar 18, 2010 at 11:22 AM
Exploring ColdFusion Component Runtime Class Properties And Serialization
@Zarko, Ha ha, you know ColdFusion is my first love ;) ... read »
Mar 18, 2010 at 11:15 AM
Exploring ColdFusion Component Runtime Class Properties And Serialization
Hi Ben, nice to have you back! I already gave up on you, thinking you'll write about jQuery and iPhone for the rest our our lives! :) ... read »