Results 1 to 2 of 2

Thread: document.body.appendChild(canvasImage2);

  1. #1
    Join Date
    Dec 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default document.body.appendChild(canvasImage2);

    Quote Originally Posted by jscheuer1 View Post
    Your code works here in IE 7. Perhaps you were trying to execute it before the browser had parsed the body (before onload). If so, there would technically be no body to append to.

    Working demo:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script type="text/javascript">
    onload = function(){
    var divg = document.createElement("div");
    divg.appendChild(document.createTextNode("New DIV"));
    document.body.appendChild(divg);
    };
    </script>
    </head>
    <body>
    
    </body>
    </html>
    ##########################################################

    Hello,

    With the following code I intend to create a new image with the text merge on it...
    Can you tell me what I am doing wrong?
    Thanks in advance,
    Ricardo

    ##########################################################

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset=utf-8 />
    <title>Image and text</title>
    
    <style id="jsbin-css">
    *{margin:0; padding:0;}
    body{
    background:#000;
    color:#fff;
    }
    canvas{  
      border-left:2px solid #000;
      border-right:2px solid #000;
      border-top:2px solid #000;
      -webkit-transition:1s;
      transition:1s;
      margin:40px auto;
      display:block;
    }
    </style>
    </head>
    <body>
        
    <script>
    var canvas  = document.createElement('canvas'),
    	ctx = canvas.getContext('2d');
    
    
    canvas.width  = 500;
    canvas.height = 500;
    canvas.style.borderRadius = "15px";
    document.body.appendChild(canvas); // Add canvas to DOM
    
    
    // GRAPHICS
    var canvasImage = new Image();
    canvasImage.src = 'S1110.jpg';
    
    
    // GRAPHICS TO CANVAS ///////////////////
    
    function drawCanvas(){ 
    	ctx.drawImage(canvasImage, 0, 0);
    	
    	ctx.font = '90px Calibri';
    	ctx.textAlign = 'center';
    	ctx.fillStyle = 'rgba(70, 70, 255, 0.7)';
    	ctx.fillText('Some text', canvas.width/2, canvas.height/2);
    }
    
    drawCanvas();
    
    var canvasImage2 = new Image();
    canvasImage2.src = drawCanvas();
    
    document.body.appendChild(canvasImage2);
    
    document.images[0].src;
    
    </script>
    
    <a href="send.php" id="send-button" class="btn btn-info">send</a>
    
    </body>
    </html>
    Last edited by jscheuer1; 12-13-2013 at 02:50 PM. Reason: Format

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    You're not really doing anything wrong. More like doing the right things in a confused manner. The only really wrong thing that jumps out at me is:

    Code:
    canvasImage2.src = drawCanvas();
    The drawCanvas function has no return value, so that's the same as:

    Code:
    canvasImage2.src = 'undefined';
    So of course you see no image. The first image may or may not appear depending upon the browser and whether or not the image is loaded yet. Try (using the onload event of the image to trigger the drawing of the canvas):

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset=utf-8 />
    <title>Image and text</title>
    
    <style id="jsbin-css">
    *{margin:0; padding:0;}
    body{
    background:#000;
    color:#fff;
    }
    canvas{  
      border-left:2px solid #000;
      border-right:2px solid #000;
      border-top:2px solid #000;
      -webkit-transition:1s;
      transition:1s;
      margin:40px auto;
      display:block;
    }
    </style>
    </head>
    <body>
        
    <script>
    var canvas  = document.createElement('canvas'),
    	ctx = canvas.getContext('2d');
    
    
    canvas.width  = 500;
    canvas.height = 500;
    canvas.style.borderRadius = "15px";
    document.body.appendChild(canvas); // Add canvas to DOM
    
    
    // GRAPHICS
    var canvasImage = new Image();
    
    
    // GRAPHICS TO CANVAS ///////////////////
    
    function drawCanvas(){ 
    	ctx.drawImage(canvasImage, 0, 0);
    	
    	ctx.font = '90px Calibri';
    	ctx.textAlign = 'center';
    	ctx.fillStyle = 'rgba(70, 70, 255, 0.7)';
    	ctx.fillText('Some text', canvas.width/2, canvas.height/2);
    }
    
    canvasImage.onload = drawCanvas;
    canvasImage.src = 'S1110.jpg';
    
    
    
    </script>
    
    <a href="send.php" id="send-button" class="btn btn-info">send</a>
    
    </body>
    </html>
    Here's a demo:

    http://home.comcast.net/~jscheuer1/s...its/canvas.htm
    Last edited by jscheuer1; 12-13-2013 at 05:53 PM. Reason: add workable code and demo
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Similar Threads

  1. Document Text Sizer - Not working when substitute "body" with another tag name
    By vlane95678 in forum Dynamic Drive scripts help
    Replies: 1
    Last Post: 10-31-2013, 05:26 PM
  2. document.body.appendChild doesn't work
    By Barto in forum JavaScript
    Replies: 6
    Last Post: 04-27-2009, 01:35 AM
  3. DOM createElement/appendChild/etc generator?
    By jlizarraga in forum JavaScript
    Replies: 7
    Last Post: 10-26-2008, 11:37 AM
  4. Error on 'appendChild' to DIV
    By eldad87 in forum JavaScript
    Replies: 0
    Last Post: 05-11-2007, 12:17 PM
  5. appendchild() IE vs Firefox
    By no1uknow in forum JavaScript
    Replies: 1
    Last Post: 11-08-2005, 04:04 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •