473,804 Members | 2,124 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

dynamic addition and manipulation of image using javascript

3 New Member
i am trying to dynamically load the image of a bullet and then manipulate it using a looping function( with setTimeout() and clearTimeout() methods).
here's the code>>>>

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <script type="text/javascript" language="JavaScript">
  4. var timer;
  5. function keyPress( )
  6. {
  7.     var bullet=new Image( );
  8.     var bowser=document.getElementById("Bowser");
  9.     bullet.setAttribute("style","position:absolute;left:0 px;top:0 px;");
  10.     bullet.style.top=bowser.style.top;
  11.     bullet.style.left=bowser.style.left;
  12.     bullet.setAttribute("src","bullet1.jpg");
  13.     document.body.appendChild(bullet);
  14.     fire(bullet);
  15. }
  16.  
  17. function fire(bullet)
  18. {
  19.     var x=parseInt(bullet.style.left);
  20.     var y=x+8;
  21.     bullet.style.left=y+" px";
  22.     if(x>1024)
  23.     {
  24.         clearTimeout(timer);
  25.         document.body.removeChild(bullet);    
  26.     }
  27.     timer=setTimeout("fire("+bullet+")",50);    
  28. }
  29. </script>
  30. </head>
  31. <body onkeypress="javascript:keyPress( )">
  32. <img class="other" id="Bowser" src="D:\Documents and Settings\Administrator\My Documents\My Pictures\Images\mario\Bowser.gif" altText="Bowser" style="position:absolute;top:500 px;left:12 px" />
  33. </body>
  34. </html>
the script loads the image alright but gives an error soon after.

OS: Windows XP Pro and IE 7
lang: JavaScript
error code: code 0
message: 'object' is undefined
line: line 1 char 1
Oct 6 '08 #1
3 2188
RamananKalirajan
608 Contributor
Can u please explain ur requirement a little bit more. I worked with ur code & i found the problem, the element "Bullet" u used in the fire function is a problem. I replaced with some other method and find the code was running but the fire function was called infinitely. Can u please tell what do u want to do or what are u trying to do.

The following code was the one i tried.
[HTML]<html>
<head>
<script type="text/javascript">
var timer;
var counter=0;
function keyPress( )
{
var bullet=new Image( );
var bowser=document .getElementById ("Bowser");
bullet.setAttri bute("style","p osition:absolut e;left:0 px;top:0 px;");
bullet.style.to p=bowser.style. top;
bullet.style.le ft=bowser.style .left;
bullet.setAttri bute("src","bul let1.jpg");
var bulletId = "Bullet"+counte r;
counter++;
bullet.setAttri bute("id",bulle tId);
document.body.a ppendChild(bull et);
fire(counter);
}

function fire(count)
{
var objId = "Bullet"+(--count);
alert(objId);
var bulet= document.getEle mentById(objId) ;
var x=parseInt(bule t.style.left);
var y=x+80;
bulet.style.cli entX=y+" px";
if(x>1024)
{
clearTimeout(ti mer);
document.body.r emoveChild(objI d);
}
setTimeout("fir e("+counter+")" ,200);
}

</script>
</head>
<body onkeypress="jav ascript:keyPres s( )">
<img class="other" id="Bowser" src="Bowser.gif " altText="Bowser " style="position :absolute;top:5 00 px;left:12 px" />
</body>
</html>[/HTML]

Regards
Ramanan Kalirajan
Oct 6 '08 #2
pdeb1234
3 New Member
Can u please explain ur requirement a little bit more. I worked with ur code & i found the problem, the element "Bullet" u used in the fire function is a problem. I replaced with some other method and find the code was running but the fire function was called infinitely. Can u please tell what do u want to do or what are u trying to do.

The following code was the one i tried.
[HTML]<html>
<head>
<script type="text/javascript">
var timer;
var counter=0;
function keyPress( )
{
var bullet=new Image( );
var bowser=document .getElementById ("Bowser");
bullet.setAttri bute("style","p osition:absolut e;left:0 px;top:0 px;");
bullet.style.to p=bowser.style. top;
bullet.style.le ft=bowser.style .left;
bullet.setAttri bute("src","bul let1.jpg");
var bulletId = "Bullet"+counte r;
counter++;
bullet.setAttri bute("id",bulle tId);
document.body.a ppendChild(bull et);
fire(counter);
}

function fire(count)
{
var objId = "Bullet"+(--count);
alert(objId);
var bulet= document.getEle mentById(objId) ;
var x=parseInt(bule t.style.left);
var y=x+80;
bulet.style.cli entX=y+" px";
if(x>1024)
{
clearTimeout(ti mer);
document.body.r emoveChild(objI d);
}
setTimeout("fir e("+counter+")" ,200);
}

</script>
</head>
<body onkeypress="jav ascript:keyPres s( )">
<img class="other" id="Bowser" src="Bowser.gif " altText="Bowser " style="position :absolute;top:5 00 px;left:12 px" />
</body>
</html>[/HTML]

Regards
Ramanan Kalirajan

what i am trying to do is to make my character "Bowser" fire bullets on pressing a key. the fire function mimics a thread. the bullet image should load dynamically and will be moved horizontally across the screen after loading.
note that i can do this by adding a few html <img/> tags with width and height set to 0. but if i want rapid firing of an unknown number of bullets i must load them dynamically the reason i am calling setTimeout("fir e("+bullet+")", 50)
is that a bullet must continue moving till it reaches the other or right end of the screen. Once there, it will be removed by document.body.r emoveChild() method.
however i realise that i can give each bullet a unique id as you have done. Thanks for the suggestion.

regards
pdeb1234
Oct 6 '08 #3
RamananKalirajan
608 Contributor
what i am trying to do is to make my character "Bowser" fire bullets on pressing a key. the fire function mimics a thread. the bullet image should load dynamically and will be moved horizontally across the screen after loading.
note that i can do this by adding a few html <img/> tags with width and height set to 0. but if i want rapid firing of an unknown number of bullets i must load them dynamically the reason i am calling setTimeout("fir e("+bullet+")", 50)
is that a bullet must continue moving till it reaches the other or right end of the screen. Once there, it will be removed by document.body.r emoveChild() method.
however i realise that i can give each bullet a unique id as you have done. Thanks for the suggestion.

regards
pdeb1234

Hi Pdeb1234,
this is my suggestion not the final solution or something. Why dont u use an object with marquee. once the bullet reaches from left to right it can be stopped. The object can be cloned as per the keypress this is a suggestion.


Regards
Ramanan Kalirajan
Oct 6 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
1795
by: Dante | last post by:
My company stores thousands of images in a SQL 200 database (image datatype). We have a need to manipulate these images as they are presented according to different situations. For example - different logos need to be inserted at the bottom of the image according to the requesting party. Any thoughts on the subject are much appreciated. Dante
7
6773
by: JavaScriptRocks | last post by:
I've been trying to imitate / reverse engineer the add attachment feature in gmail composer. I managed to do it to say about 80% but its giving me trouble in IE on WinXP-Sp2. I am using PHP to do the upload. It works well on Firefox/DeerPark, but in IE, the file selected just vanishes. You can verify it by commenting the lines marked "//IE Trouble". Commenting those lines will remove IE specific code, except the el.click() whick sends the...
3
2727
by: JOSEPHINE ALVAREZ | last post by:
I have this code that I want to use to do a rollover. However, because the company I am doing it for is continually changing the text, I want to be able to use dynamic text to change the text on the fly, and still have the rollover work. I have taken the text off of the buttons, but cannot figure out how to do it with dynamic text using javascript and html. For example, in the columns of this row, I want to put "About...
9
2152
by: Job | last post by:
Hi, I would like to find out what ASP/ASP.net can do with image manipulation. Does ASP have built in functions (eg. after upload to server) to manipulate images, like rotate, scale, crop etc.? Or are 3rd party solutions needed to do this? I am a php/codfusion programmer and know nothing about asp, so I'm hoping somebody here can help me out.
9
3379
by: zacariaz | last post by:
I dont know, and i dont much like javascript, however, i am told that the only way to do want i want to do, is with javascript, so here goes. zoom and cut is the only features i need. 1. the image that need manipulating is places on the server. dont need js for that ;-) 2. the client has the oppotunity to manipulate the image. cut and zoom. 3. the image i saved on the server.
10
3493
by: Pulzar | last post by:
Hi there, I want to show a simple image on a web page, and allow the viewer to select and change one of the colours used in the image, and immediately preview the result. I'd like to keep the image processing away from the server to make the colour selection/preview process quicker, and I also don't want to pre-generate all possible images as there are too many colours that can be selected. Does JS (or any JS libraries) provide image...
1
6069
by: avicalc | last post by:
I am using HTML checkboxes and JavaScript to toggle on and off, a number of absolutely positioned transparent images located on directly top of each other with different z-indexes. In addition, all of my images share a single client-side image map which is also dynamically generated (using XmlHttpRequest) based on the status of each checkbox. I collect my responses from XmlHttpRequest and inject them into the innerHTML property of a div to...
9
2989
by: pbd22 | last post by:
Hi. This is just a disaster management question. I am using XMLHTTP for the dynamic loading of content in a very crucial area of my web site. Same as an IFrame, but using XMLHTTP and a DIV. I got the core of the javascript from here: http://www.dynamicdrive.com/dynamicindex17/ajaxcontent.htm I noticed in the demo that sometimes the content takes a long
1
3395
by: neovantage | last post by:
Hey all, I am using a PHP script which creates headings at run time in a sense at page execution. I am stuck a with a very little problem which i am sure i will have the solution from experts. The problem is when it creates transparent PNG format image then and it pixel ate the image. e.g. If i am using a gradient in background then it vary in color range. Now when i used that php script it generates image successfully but it pixel ate...
0
9716
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9595
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10354
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10359
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10101
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9177
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5536
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5675
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3005
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.