简体   繁体   English

将背景图像设置为画布并使用文本+图像导出

[英]Set background image to canvas and export it with text+image

I am trying to solving one problem , I have a textarea which works fine and exporting text to canvas , but I don't know how to achieve the same effect and add background image option to this, it could be a static background image from the beginning, here is script .When i was trying to set background image via html , i had background image behind the text , but when i was exporting i had only text in my png.我正在尝试解决一个问题,我有一个可以正常工作并将文本导出到画布的 textarea,但我不知道如何实现相同的效果并为此添加背景图像选项,它可能是来自开始,这里是脚本。当我试图通过 html 设置背景图像时,我在文本后面有背景图像,但是当我导出时,我的 png 中只有文本。

   var canvas = $('#canvas');
var ctx = canvas.get(0).getContext('2d');



canvas.width = 600;
canvas.height = 400;



$('#submit').click(function (e) {
  e.preventDefault();


  var text = $('#text').val(),
      fontSize = parseInt($('#font-size').val()),
      width = parseInt($('#width').val()),
      lines = [],
      line = '',
      lineTest = '',
      words = text.split(' '),
      currentY = 0;
  
  ctx.font = fontSize + 'px Arial';
    
  for (var i = 0, len = words.length; i < len; i++) {
    lineTest = line + words[i] + ' ';
    
    // Check total width of line or last word
    if (ctx.measureText(lineTest).width > width) {
      // Calculate the new height
      currentY = lines.length * fontSize + fontSize;

      // Record and reset the current line
      lines.push({ text: line, height: currentY });
      line = words[i] + ' ';
    } else {
      line = lineTest;
    }
  }


  
  // Catch last line in-case something is left over
  if (line.length > 0) {
    currentY = lines.length * fontSize + fontSize;
    lines.push({ text: line.trim(), height: currentY });
  }
  
  // Visually output text
  ctx.clearRect(0, 0, 500, 500);
  for (var i = 0, len = lines.length; i < len; i++) {
    ctx.fillText(lines[i].text, 0, lines[i].height);
  }

  var canvasCtx = document.getElementById("canvas").getContext('2d');
  var img = document.getElementById("yourimg");
  canvasCtx.drawImage(img,x,y);
  
  var img = new Image();
  img.onload = function(){
      canvasCtx.drawImage(img,x,y);
  };
  
img.src = "https://i.stack.imgur.com/7Whkw.png";

});

i have solved it!我已经解决了!

var canvas = $('#canvas');
var ctx = canvas.get(0).getContext('2d');
    
    
    
    
    var canvas = document.getElementById('canvas');
    var context = canvas.getContext('2d');
    
    
    var img = new Image();
    var height = 600;
    var width = 600;
    img.onload = function() {
      context.save();
        context.rect(0, 0, 200, 200);//Здесь первый 0=X
    
        context.drawImage(img,0, 0,width,height);//Первый 0=X
        context.restore();
    };
    img.src = 'https://i.stack.imgur.com/5uvJN.png';
    
    
    $('#submit').click(function (e) {
      e.preventDefault();
    
    
    
      
      var text = $('#text').val(),
          fontSize = parseInt($('#font-size').val()),
          width = parseInt($('#width').val()),
          lines = [],
          line = '',
          lineTest = '',
          words = text.split(' '),
          currentY = 0;
      
      ctx.font = fontSize + 'px Arial';
        
      for (var i = 0, len = words.length; i < len; i++) {
        lineTest = line + words[i] + ' ';
        
        // Check total width of line or last word
        if (ctx.measureText(lineTest).width > width) {
          // Calculate the new height
          currentY = lines.length * fontSize + fontSize;
    
          // Record and reset the current line
          lines.push({ text: line, height: currentY });
          line = words[i] + ' ';
        } else {
          line = lineTest;
        }
      }
    
    
      
      // Catch last line in-case something is left over
      if (line.length > 0) {
        currentY = lines.length * fontSize + fontSize;
        lines.push({ text: line.trim(), height: currentY });
      }
      
      // Visually output text
      ctx.clearRect(0, 500, 500, 500);
      for (var i = 0, len = lines.length; i < len; i++) {
        ctx.fillText(lines[i].text, 410, lines[i].height);
      }
    
    
    });

one more question how i can set padding top for textarea text on canvas?还有一个问题,我如何在画布上为 textarea 文本设置填充顶部?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM