943,673 Members | Top Members by Rank

Ad:
Apr 22nd, 2009
0

Simple Javascript Graphics

Expand Post »
I want a box/frame/applet on my html webpage that will display an image and depending on where the user clicks i want text to popup/disappear somewhere else in the image. Does anyone know how to do this in javascript. Sorry Im new to web development and couldn't figure out how to do it in php. Thanks!
Similar Threads
Reputation Points: 11
Solved Threads: 2
Light Poster
kinger29 is offline Offline
35 posts
since Mar 2008
Apr 22nd, 2009
-1

Re: Simple Javascript Graphics

The above can be done through applet and no need for JavaScript. However if you insist on JavaScript solution of which I'm not sure can be done, then you are in wrong section (Java is not JavaScript) press Flag Bad Post option next to your original post and write "Please move to JavaScript section".
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 871
Code tags enforcer
peter_budo is offline Offline
6,654 posts
since Dec 2004
Apr 25th, 2009
0

Re: Simple Javascript Graphics

Kinger,

I'm away from home at the mo' and it's getting late. I'll post a DHTML solution a.s.a.p. It will employ HTML, CSS and JavaScript.

Airshow
Sponsor
Reputation Points: 302
Solved Threads: 358
WiFi Lounge Lizard
Airshow is offline Offline
2,524 posts
since Apr 2009
Apr 26th, 2009
0

Re: Simple Javascript Graphics

Kinger,

Here are three ways to do what you want using HTML and JavaScript, involving a mixture of mouseover and click events to cause text to be displayed.

Paste all the following code into a new text file - eg. test.html - then browse in your browser.

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <head>
  4. <title>Untitled</title>
  5. <style type="text/css">
  6. /* General styles */
  7. body { font-family:verdana,arial,helvetics,sans-serif; }
  8. h1 { margin:0; font-size:14pt;}
  9. h2 { margin:0; font-size:12pt; }
  10. .method { margin:30px 0; }
  11. p.note { margin:3px 0 0 0; font-size:8pt; }
  12. p.sig { margin:12px 0 0 0; font-size:14pt; font-style:italic; }
  13.  
  14. /* Styles for Method 1 */
  15. #altImageWrapper { margin:3px 0 0 0; }
  16. #altImageWrapper img { border:0; }
  17.  
  18. /* Styles for Method 2 */
  19. #imageWrapper { position:relative; margin:6px 0 0 0; }
  20. #imageWrapper img { border-width:0; }
  21. #imageWrapper .popUpTxt { position:absolute; display:none; padding:0px 3px; font-weight:bold; background-color:#FFFFFF; font-size:10pt; border:1px solid #999999; }/* Note: This causes all three text divs to be absolutely positioned and initially hidden */
  22. #imageWrapper #popUpTxt1 { left:30px; top:35px; color:#800868; }
  23. #imageWrapper #popUpTxt2 { left:78px; top:35px; color:#800868; }
  24. #imageWrapper #popUpTxt3 { left:170px; top:35px; color:#F09000; }
  25.  
  26. /* Styles for Method 3 */
  27. #imageWrapper2 { margin:6px 0 0 0; }
  28. #imageWrapper2 img { border:0; }
  29. #popUpTxtWrapper { margin:12px 0 0 0; width:120px; height:1.0em; font-size:14px; font-weight:bold; text-align:center; border:2px solid #999999; }
  30. </style>
  31.  
  32. <script language="JavaScript" type="text/javascript">
  33. //No JavaScript required for Method 1
  34.  
  35. //For Method 2
  36. function showHideTxt(n, b_show){
  37. if(!n) return false;
  38. b_show = (!b_show) ? false : true;
  39. var wrapper = (document.getElementById) ? document.getElementById('imageWrapper') : document.all.imageWrapper;
  40. for(var i=1; i<=3; i++){
  41. var txtWrapper = (document.getElementById) ? document.getElementById('popUpTxt'+i) : document.all['popUpTxt'+i];
  42. txtWrapper.style.display = (b_show && i==n) ? 'block' : 'none';
  43. }
  44. return false;
  45. }
  46.  
  47. //For Method 3
  48. function showTxt(n, txt, color){
  49. if(!n) return false;
  50. txt = (!txt) ? '- - - - -' : txt;
  51. color = (!color) ? '#000000' : color;
  52. var txtWrapper = (document.getElementById) ? document.getElementById('popUpTxtWrapper') : document.all['popUpTxtWrapper'];
  53. txtWrapper.innerHTML = txt;
  54. txtWrapper.style.color = color;
  55. return false;
  56. }
  57. </script>
  58. </head>
  59.  
  60. <body>
  61. <h1>Show/hide text in response to User Events with an Image</h1>
  62. <p class="note">Three methods. In each method there is a "hot-spot" over the "D", "N" and "B" of <b>Daniweb</b>.</p>
  63.  
  64. <div class="method">
  65. <h2>Method 1: Imagemap with "alt" Text</h2>
  66. <p class="note">Place <b>mouse over</b> hot-spot to show text; move <b>mouse off</b> hot-spot to hide.</p>
  67. <p class="note">This is the easiest method by far as it involves only HTML, no Javascript.</p>
  68. <div id="altImageWrapper" class="method">
  69. <img src="http://www.daniweb.com/alphaimages/logo/logo.gif" useMap="#altTxt" />
  70. </div>
  71. <map name="altTxt">
  72. <area shape="rect" coords="30,0,58,30" href="" alt="D D D D D" onclick="this.blur(); return false">
  73. <area shape="rect" coords="78,0,106,30" href="" alt="N N N N N" onclick="this.blur(); return false">
  74. <area shape="rect" coords="170,0,195,30" href="" alt="B B B B B" onclick="this.blur(); return false">
  75. </map>
  76. </div>
  77.  
  78. <div class="method">
  79. <h2>Method 2: Imagemap with DHTML (text over image)</h2>
  80. <p class="note"><b>Click</b> hot-spot to show text; move <b>mouse off</b> hot-spot to hide.</p>
  81. <p class="note">Text written directly into HTML divs; text colors defined in CSS</p>
  82. <div id="imageWrapper">
  83. <img src="http://www.daniweb.com/alphaimages/logo/logo.gif" useMap="#dhtmlTxt1" />
  84. <div id="popUpTxt1" class="popUpTxt">D D D D D</div>
  85. <div id="popUpTxt2" class="popUpTxt">N N N N N</div>
  86. <div id="popUpTxt3" class="popUpTxt">B B B B B</div>
  87. </div>
  88. <map name="dhtmlTxt1">
  89. <area shape="rect" coords="30,0,58,30" href="" onclick="return showHideTxt(1, true)" onMouseOut="return showHideTxt(1, false)">
  90. <area shape="rect" coords="78,0,106,30" href="" onclick="return showHideTxt(2, true)" onMouseOut="return showHideTxt(2, false)">
  91. <area shape="rect" coords="170,0,195,30" href="" onclick="return showHideTxt(3, true)" onMouseOut="return showHideTxt(3, false)">
  92. </map>
  93. </div>
  94.  
  95. <div class="method">
  96. <h2>Method 3: Imagemap with DHTML (text outside image)</h2>
  97. <p class="note">Place <b>mouse over</b> hot-spot to show text; move <b>mouse off</b> hot-spot to hide.</p>
  98. <p class="note">Text and text color determined dynamically in event handler calls</p>
  99. <div id="imageWrapper2">
  100. <img src="http://www.daniweb.com/alphaimages/logo/logo.gif" useMap="#dhtmlTxt2" />
  101. </div>
  102. <div id="popUpTxtWrapper">- - - - -</div>
  103. <map name="dhtmlTxt2">
  104. <area shape="rect" coords="30,0,58,30" href="" onmouseover="this.blur(); return showTxt(1, 'D D D D D', '#800868')" onMouseOut="return showTxt(1, '')" onclick="this.blur(); return false">
  105. <area shape="rect" coords="78,0,106,30" href="" onmouseover="this.blur(); return showTxt(2, 'N N N N N', '#800868')" onMouseOut="return showTxt(2, '')" onclick="this.blur(); return false">
  106. <area shape="rect" coords="170,0,195,30" href="" onmouseover="this.blur(); return showTxt(3, 'B B B B B', '#F09000')" onMouseOut="return showTxt(3, '')" onclick="this.blur(); return false">
  107. </map>
  108. </div>
  109.  
  110. <p class="note">It shoud be fairly easy to mix and match methods 2 and 3 as necessary for the desired effect.</p>
  111. <p class="sig">Airshow</p>
  112.  
  113. </body>
  114. </html>
Personally I would say that an Applet would be a sledgehammer-to-crack-a-nut but, there again, I am not an Applet person.

Airshow
Sponsor
Reputation Points: 302
Solved Threads: 358
WiFi Lounge Lizard
Airshow is offline Offline
2,524 posts
since Apr 2009
Apr 26th, 2009
0

Re: Simple Javascript Graphics

W3 image map
html Syntax (Toggle Plain Text)
  1. <img src="image.jpg" width="145" height="126" alt="image" usemap="#map" />
  2. <map name="map">
  3. <area shape="rect" coords="0,0,82,126" href="1.htm" alt="1" onclick onmouseover onmouseout ondblclick />
  4. <area shape="circle" coords="90,58,3" href="2.htm" alt="2" />
  5. <area shape="circle" coords="124,58,8" href="3.htm" alt="3" />
  6. </map>
Last edited by almostbob; Apr 26th, 2009 at 11:09 am.
Reputation Points: 562
Solved Threads: 368
Posting Maven
almostbob is offline Offline
2,970 posts
since Jan 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: How to disbale Close(X) button in title bar
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: Form submit doesn't works in Firefox





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC