2017-08-11 12 views
1

JavaScriptを使用している場合のみ、私のウェブページに「無料ドローボックス」を挿入しようとしています。私は自分の「フリー・ドロー・ボックス・スクリプト」をHTMLのあるページで動作させることができましたが、代わりにJavaScriptを介してHTMLを挿入することでこれを達成したいと思います。私はコード全体のスニペットを含んでいますが、問題のコードは「HTMLの挿入」の下に注釈されています。私のミスはどこですか?JavaScriptを使用したJavaScriptの挿入

<html> 
 

 
    <script type="text/javascript"> 
 

 
    /*Free Draw Box Script*/ 
 
    var canvas, ctx, flag = false, 
 
     prevX = 0, 
 
     currX = 0, 
 
     prevY = 0, 
 
     currY = 0, 
 
     dot_flag = false; 
 

 
    var x = "black", 
 
     y = 2; 
 
    
 
    function init() { 
 
     canvas = document.getElementById('can'); 
 
     ctx = canvas.getContext("2d"); 
 
     w = canvas.width; 
 
     h = canvas.height; 
 
    
 
     canvas.addEventListener("mousemove", function (e) { 
 
      findxy('move', e) 
 
     }, false); 
 
     canvas.addEventListener("mousedown", function (e) { 
 
      findxy('down', e) 
 
     }, false); 
 
     canvas.addEventListener("mouseup", function (e) { 
 
      findxy('up', e) 
 
     }, false); 
 
     canvas.addEventListener("mouseout", function (e) { 
 
      findxy('out', e) 
 
     }, false); 
 
    } 
 
    
 
    function draw() { 
 
     ctx.beginPath(); 
 
     ctx.moveTo(prevX, prevY); 
 
     ctx.lineTo(currX, currY); 
 
     ctx.strokeStyle = x; 
 
     ctx.lineWidth = y; 
 
     ctx.stroke(); 
 
     ctx.closePath(); 
 
    } 
 
    
 
    function erase() { 
 
     var m = confirm("Are you sure you want to clear the Signature?"); 
 
     if (m) { 
 
      ctx.clearRect(0, 0, w, h); 
 
      document.getElementById("canvasimg").style.display = "none"; 
 
     } 
 
    } 
 
    
 
    function save() { 
 
     document.getElementById("canvasimg").style.border = "2px solid"; 
 
     var dataURL = canvas.toDataURL(); 
 
     document.getElementById("canvasimg").src = dataURL; 
 
     document.getElementById("canvasimg").style.display = "inline"; 
 
    } 
 
    
 
    function findxy(res, e) { 
 
     if (res == 'down') { 
 
      prevX = currX; 
 
      prevY = currY; 
 
      currX = e.clientX - canvas.offsetLeft; 
 
      currY = e.clientY - canvas.offsetTop; 
 
    
 
      flag = true; 
 
      dot_flag = true; 
 
      if (dot_flag) { 
 
       ctx.beginPath(); 
 
       ctx.fillStyle = x; 
 
       ctx.fillRect(currX, currY, 2, 2); 
 
       ctx.closePath(); 
 
       dot_flag = false; 
 
      } 
 
     } 
 
     if (res == 'up' || res == "out") { 
 
      flag = false; 
 
     } 
 
     if (res == 'move') { 
 
      if (flag) { 
 
       prevX = currX; 
 
       prevY = currY; 
 
       currX = e.clientX - canvas.offsetLeft; 
 
       currY = e.clientY - canvas.offsetTop; 
 
       draw(); 
 
      } 
 
     } 
 
    } 
 

 
    /*Insert HTML*/ 
 
    document.body.innerHTML += ' 
 
     <body onload="init()"> 
 
      <canvas id="can" width="800" height="200" 
 
      style="position:absolute;top:10%;left:10%;border:1px solid;"> 
 
      </canvas> 
 

 
      <img id="canvasimg" style="position:absolute;top:10%; 
 
      left:52%;" style="display:none;"> 
 
      <input type="button" value="clear" id="clr" size="23" 
 
      onclick="erase()" style="position:absolute;top:55%; 
 
      left:15%;"> 
 
     </body> 
 
    '; 
 

 
    </script> 
 

 
    <body> 
 
     <p></p> 
 
    </body> 
 

 
    </html>

+0

「/」引用符で囲まれた文字列は、複数行をサポートしていない、あなたがこのような場合には '使用する必要が – tibetty

+0

jQueryはHTMLを挿入するために絶対に必要ではありませんg JavaScript。 JavaScriptには、外部のライブラリやフレームワークなしでこれを行うための、幅広くサポートされている組み込みの機能が豊富に用意されています。 – Adrian

答えて

1

  1. 使用template literals HTML文字列のために。だから、あなたはどうなるのキャンバスを作成します
  2. またを使用して、文字列のbody要素を入力してください。

下記参照デモ:

var canvas,ctx,flag=!1,prevX=0,currX=0,prevY=0,currY=0,dot_flag=!1;var x="black",y=2;function init(){canvas=document.getElementById('can');ctx=canvas.getContext("2d");w=canvas.width;h=canvas.height;canvas.addEventListener("mousemove",function(e){findxy('move',e)},!1);canvas.addEventListener("mousedown",function(e){findxy('down',e)},!1);canvas.addEventListener("mouseup",function(e){findxy('up',e)},!1);canvas.addEventListener("mouseout",function(e){findxy('out',e)},!1)} 
 
function draw(){ctx.beginPath();ctx.moveTo(prevX,prevY);ctx.lineTo(currX,currY);ctx.strokeStyle=x;ctx.lineWidth=y;ctx.stroke();ctx.closePath()} 
 
function erase(){var m=confirm("Are you sure you want to clear the Signature?");if(m){ctx.clearRect(0,0,w,h);document.getElementById("canvasimg").style.display="none"}} 
 
function save(){document.getElementById("canvasimg").style.border="2px solid";var dataURL=canvas.toDataURL();document.getElementById("canvasimg").src=dataURL;document.getElementById("canvasimg").style.display="inline"} 
 
function findxy(res,e){if(res=='down'){prevX=currX;prevY=currY;currX=e.clientX-canvas.offsetLeft;currY=e.clientY-canvas.offsetTop;flag=!0;dot_flag=!0;if(dot_flag){ctx.beginPath();ctx.fillStyle=x;ctx.fillRect(currX,currY,2,2);ctx.closePath();dot_flag=!1}} 
 
if(res=='up'||res=="out"){flag=!1} 
 
if(res=='move'){if(flag){prevX=currX;prevY=currY;currX=e.clientX-canvas.offsetLeft;currY=e.clientY-canvas.offsetTop;draw()}}} 
 

 
/*Insert HTML*/ 
 
document.body.outerHTML += `<body onload="init()"> 
 
      <canvas id="can" width="800" height="200" 
 
    style="position:absolute;top:10%;left:10%;border:1px solid;"> 
 
      </canvas> 
 
      <img id="canvasimg" style="position:absolute;top:10%; 
 
      left:52%;" style="display:none;"> 
 
      <input type="button" value="clear" id="clr" size="23" 
 
      onclick="erase()" style="position:absolute;top:55%; 
 
      left:15%;"> 
 
     </body>`;

+1

kukkuz、あなたが上で提供したスニペットは、魅力のように働いた!助けてくれてありがとう! – aglowacki

0

<html> 
 

 
    <script type="text/javascript"> 
 

 
    /*Free Draw Box Script*/ 
 
    var canvas, ctx, flag = false, 
 
     prevX = 0, 
 
     currX = 0, 
 
     prevY = 0, 
 
     currY = 0, 
 
     dot_flag = false; 
 

 
    var x = "black", 
 
     y = 2; 
 
    
 
    function init() { 
 
     canvas = document.getElementById('can'); 
 
     ctx = canvas.getContext("2d"); 
 
     w = canvas.width; 
 
     h = canvas.height; 
 
    
 
     canvas.addEventListener("mousemove", function (e) { 
 
      findxy('move', e) 
 
     }, false); 
 
     canvas.addEventListener("mousedown", function (e) { 
 
      findxy('down', e) 
 
     }, false); 
 
     canvas.addEventListener("mouseup", function (e) { 
 
      findxy('up', e) 
 
     }, false); 
 
     canvas.addEventListener("mouseout", function (e) { 
 
      findxy('out', e) 
 
     }, false); 
 
    } 
 
    
 
    function draw() { 
 
     ctx.beginPath(); 
 
     ctx.moveTo(prevX, prevY); 
 
     ctx.lineTo(currX, currY); 
 
     ctx.strokeStyle = x; 
 
     ctx.lineWidth = y; 
 
     ctx.stroke(); 
 
     ctx.closePath(); 
 
    } 
 
    
 
    function erase() { 
 
     var m = confirm("Are you sure you want to clear the Signature?"); 
 
     if (m) { 
 
      ctx.clearRect(0, 0, w, h); 
 
      document.getElementById("canvasimg").style.display = "none"; 
 
     } 
 
    } 
 
    
 
    function save() { 
 
     document.getElementById("canvasimg").style.border = "2px solid"; 
 
     var dataURL = canvas.toDataURL(); 
 
     document.getElementById("canvasimg").src = dataURL; 
 
     document.getElementById("canvasimg").style.display = "inline"; 
 
    } 
 
    
 
    function findxy(res, e) { 
 
     if (res == 'down') { 
 
      prevX = currX; 
 
      prevY = currY; 
 
      currX = e.clientX - canvas.offsetLeft; 
 
      currY = e.clientY - canvas.offsetTop; 
 
    
 
      flag = true; 
 
      dot_flag = true; 
 
      if (dot_flag) { 
 
       ctx.beginPath(); 
 
       ctx.fillStyle = x; 
 
       ctx.fillRect(currX, currY, 2, 2); 
 
       ctx.closePath(); 
 
       dot_flag = false; 
 
      } 
 
     } 
 
     if (res == 'up' || res == "out") { 
 
      flag = false; 
 
     } 
 
     if (res == 'move') { 
 
      if (flag) { 
 
       prevX = currX; 
 
       prevY = currY; 
 
       currX = e.clientX - canvas.offsetLeft; 
 
       currY = e.clientY - canvas.offsetTop; 
 
       draw(); 
 
      } 
 
     } 
 
    } 
 

 
    /*Insert HTML*/ 
 
    document.body.innerHTML += ` 
 
     <body onload="init()"> 
 
      <canvas id="can" width="800" height="200" 
 
      style="position:absolute;top:10%;left:10%;border:1px solid;"> 
 
      </canvas> 
 

 
      <img id="canvasimg" style="position:absolute;top:10%; 
 
      left:52%;" style="display:none;"> 
 
      <input type="button" value="clear" id="clr" size="23" 
 
      onclick="erase()" style="position:absolute;top:55%; 
 
      left:15%;"> 
 
     </body> 
 
    `; 
 

 
    </script> 
 

 
    <body> 
 
     <p></p> 
 
    </body> 
 

 
    </html>

0

体内文句を言わない仕事に体を追加し、そののonload関数が呼び出されません。代わりに、jsを使用してDOMを直接作成し、それをページに追加します。私はJSを使用してのようなHTMLに入れてお勧めしませんが、ここにも修正です

var can = document.createElement("canvas"); 
can.width = 800; 
can.height = 200; 
document.body.appendChild(can); 

//you can use it directly 
var ctx = can.getContext("2d"); 
関連する問題