合并画布和背景



我有一个具有GIF背景的画布。我需要用户上传图纸和BG,因此另一个用户可以看到图形与BG相关的位置。canvas.todataurl();代码仅显示用用户图的画布区域,但不显示BG。如何平坦或"合并层",因此我可以上传到数据库。

var mousePressed = false;
var lastX, lastY;
var ctx;
function InitThis() {
  ctx = document.getElementById('myCanvas').getContext("2d");
  $('#myCanvas').mousedown(function(e) {
    mousePressed = true;
    Draw(e.pageX - $(this).offset().left, e.pageY - $(this).offset().top, false);
  });
  $('#myCanvas').mousemove(function(e) {
    if (mousePressed) {
      Draw(e.pageX - $(this).offset().left, e.pageY - $(this).offset().top, true);
    }
  });
  $('#myCanvas').mouseup(function(e) {
    mousePressed = false;
  });
  $('#myCanvas').mouseleave(function(e) {
    mousePressed = false;
  });
}
function Draw(x, y, isDown) {
  if (isDown) {
    ctx.beginPath();
    ctx.strokeStyle = $('#selColor').val();
    ctx.lineWidth = $('#selWidth').val();
    ctx.lineJoin = "round";
    ctx.moveTo(lastX, lastY);
    ctx.lineTo(x, y);
    ctx.closePath();
    ctx.stroke();
  }
  lastX = x;
  lastY = y;
}
function clearArea() {
  // Use the identity matrix while clearing the canvas
  ctx.setTransform(1, 0, 0, 1, 0, 0);
  ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
}
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript" src="jscripts/JsCode.js">
  var dataURL = canvas.toDataURL();
</script>
<!--canvas drawing and upload mySQL-->
<div align="center">
  <img src="graphics/teethDrawing.gif" width="705" style="position: absolute; z-index: -1" />
  <canvas id="myCanvas" width="700" height="965" style="border:2px solid black"></canvas>
  <br />
  <br />
  <button onclick="javascript:clearArea();return false;">Clear Area</button>
  Line width :
  <select id="selWidth">
    <option value="1">1</option>
    <option value="3" selected="selected">3</option>
    <option value="5">5</option>
  </select>
  Color :
  <select id="selColor">
    <option value="black" selected="selected">black</option>
    <option value="blue">blue</option>
    <option value="red">red</option>
  </select>
</div>
</div>

未显示图像,但确实显示在页面上。

帮助

您可以在"现有画布图纸"后面添加您的背景图像。

// give the image an id so it's more easily fetched
<img id='bk' src="graphics/teethDrawing.gif" 
     width="705" style="position: absolute; z-index: -1" />
// fetch a reference to the bk image
var bk=document.getElementById('bk');
// causes new drawings to be drawn behind existing drawings
ctx.globalCompositeOperation='destination-over';
// draw the img to the canvas (behind existing lines)
ctx.drawImage(bk,0,0);
// always clean up! Reset to default.
ctx.globalCompositeOperation='source-over';

此过程将永久将BK图像添加到画布中。如果需要分开BK和帆布绘图,则可以创建第二个内存帆布,通过将BK-image和Canvas-Drawings同时绘制到内存帆布。

// create a temporary canvas 
var mem=document.createElement('canvas');
var mctx=mem.getContext('2d');
mem.width=canvas.width;
mem.height=canvas.height;
// draw the bk to the mem canvas
mctx.drawImage(bk,0,0);
// draw the main canvas to the mem canvas
mctx.drawImage(canvas,0,0);

您是否希望图纸具有高度/宽度和位置:绝对样式标签?然后,您可以将这些样式标签发送到数据库,以便其他访问者可以在z索引顶部看到图形的位置:-1 image。

<img src="drawing.jpg" style="position:absolute;left:50px;top:120px;" />

最新更新