【问题标题】:2 different canvas download as single image file2个不同的画布下载为单个图像文件
【发布时间】:2016-02-20 10:14:52
【问题描述】:

在我的应用程序中,我正在尝试将用户上传的图像与第二个画布中的徽标图像合并。当我下载图像时,我的徽标重叠在用户图像上

但我真正想要的是:

我想将 2 个单独的画布相互合并,但并排且不相互重叠。

这是我的下载画布代码:canvas_thumb 是用户正在上传的图片,canvas 是我在页面上已有的徽标图片。

function downloadCanvas() {
    var bottleCanvas = document.getElementById('canvas_thumb');
    var designCanvas = document.getElementById('canvas');
    var bottleContext = bottleCanvas.getContext('2d');
    bottleContext.drawImage(designCanvas, 0, 0);

    var dataURL = bottleCanvas.toDataURL("image/png");
    var link = document.createElement('a');
    link.download = "bottle-design.png";
    link.href = bottleCanvas.toDataURL("image/png").replace("image/png", "image/octet-stream");
    link.click();
}

请看我的例子:http://bit.ly/1SYiniX

任何帮助将不胜感激。
谢谢

【问题讨论】:

  • 当我下载图像时,我的徽标重叠在用户图像上。下面的输出:i.stack.imgur.com/n5Jvc.png 但我真正想要的是:i.stack.imgur.com/Zmx4N.png 即我想将 2 个单独的画布相互合并,但并排而不是相互重叠。
  • drawImage 最多有九个参数可以将一幅图像绘制到另一幅图像上。它不会神奇地知道把图像放在哪里,你必须把它放在正确的位置。有关drawImage 参数的信息,请参阅w3schools.com/jsref/canvas_drawimage.asp
  • @Blindman67 w3schools 越来越好,但我仍然不推荐它作为参考 -> w3fools.com。最好使用Mozilla Development Networkspec itself
  • @Andreas 我已经将新手派到 MDN 只是让他们回来告诉我他们找不到要找的东西。 w3schools 对某些人来说可能不酷,但 MDN 的导航对那些几乎没有经验的人不友好。 w3schools 关于 drawImage 的信息是准确的,而 MDN 在其页面上有错误(在查看时,不是我为什么不参考它们)我将新手送到 w3schools,因为它是一个设计更好的参考。
  • @Blindman67 如果你可以只用drawImage() 来电,我想看看。 OP 需要以他想要的输出大小的第三个画布来绘制他向我们展示的两个画布......

标签: javascript image canvas download merge


【解决方案1】:

没有两个图像和规则,没有太多信息可以解决您的特定问题。 (最大/最小图像尺寸??是否有要求的方面??或最大/最小徽标比例以避免伪影)。

我假设徽标必须保持其外观并且不能修剪或缩放。还有——上传图片只能用比logo图片高多少来表示。

用户图像不能改变纵横比,上传时必须尽可能多,上下或左右裁剪,保留中心。

所以只需创建一个正确大小的画布,在底部绘制徽标,计算适合用户图像的比例。计算适合用户图像的哪一部分。将该部分绘制到顶部。

这可确保显示用户图像的最大可能区域仍将保持上传图像的正确大小

var imageHeight = 3; // the height of the upload image in terms of
                     // the logos height.
                     // in this case the image will be three times 
                     // the height of the logo

var logoW = canvas.width;
var logoH = canvas.height;


var userImage  // this is the users image

var userW = userImage.width;
var userH = userImage.height;
var userCX = userW / 2; // center of user image
var userCY = userW / 2; // center of user image

// Get the max scale that ensures the user image will 
// fill the space provide
var userScale = Math.max(logoW / userW, logoH * (imageHeight-1) / user.height);

// use the scale to calculate the correct aspect to 
// copy from the user image in user image coordinates
var userClipW = logoW / userScale;
var userClipH = logoH * (imageHeight - 1) / userScale;

// create the new canvas
var uploadImg = document.createElement("canvas");  

// set its size
uploadImg.width = logoW;
uploadImg.height = logoH * imageHeight;

//  get drawing context
ctx = uploadImg.getContext("2d");

// draw logo at the bottom
ctx.drawImage(canvas, 0, logoH * (imageHeight-1), logoW, logoH);

// draw the user image at the top, taking what can fit from the center, assuming
// that is the requirement, only top and bottom OR left and right are clipped
ctx.drawImage(userImage, userCX - userClipW * 0.5, userCY - userClipH * 0.5, userClipW, userClipH, 0, 0, logoW, logoH * (imageHeight - 1));

// data url for upload.
var dataURL = uploadImg.toDataURL();

【讨论】:

  • 基本上我需要在 500 h 480 w 的第三画布上输出。我可以在其中放置两张图片,一张在顶部,另一张在第一张图片下方。
  • 我已经准备好要合并的画布只是不知道要使用哪个确切的代码,因为我是画布的新手
  • 而且我使用的代码没有按照我想要的方式合并画布
  • 该示例确实创建了第三张图像。它是名为uploadImg 的画布图像,我最后将其转换为dataURL。你已经给出了最终的图像尺寸,我假设徽标是 480 像素宽。如果是这样,那么获取徽标高度(底部图像)找出它进入最终图像的 500 像素的次数。即500/logo.height 然后用那个数字替换最上面的3。
猜你喜欢
  • 2015-06-15
  • 1970-01-01
  • 1970-01-01
  • 2015-07-15
  • 1970-01-01
  • 2015-08-23
  • 2023-03-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多