在 3 层画布上绘制图像并保存

drawing images on 3 layer canvas and saving(在 3 层画布上绘制图像并保存)
本文介绍了在 3 层画布上绘制图像并保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 3 层画布,我为每一层赋予了单独的 id.

I have a 3 layers of canvas, which I have given a separate id to each.

这就是我正在做的.

HTML

<div style="position:relative;">
    <canvas id="layer1" style="position: absolute; left: 0; top: 0; z-index: 0;">
    </canvas>
    <canvas id="layer2" style="position: absolute; left: 0; top: 0; z-index: 1;">
    </canvas>
    <canvas id="layer3" style="position: absolute; left: 0; top: 0; z-index: 1;">
    </canvas>
</div>

我可以在 layer1 上绘制图像

I can draw images on layer1

JS

var canvas = document.getElementById('layer1');
var context = canvas.getContext('2d');
context.canvas.width  = window.innerWidth;
context.canvas.height = window.innerHeight*0.7;
var imageObj = new Image();
var imageObj1 = new Image();

imageObj.onload = function() {
    context.drawImage(imageObj, 0, 0,context.canvas.width,context.canvas.height*0.9);
    context.drawImage(imageObj1, x1, y1,6,canvasHeight);    
};

imageObj1.src='vertical.png';
imageObj.src = 'horizontal.png';

如何在所有图层上绘制图像并创建单个 canvas.toDataUrl ?

How will I draw images on all the layers and create single canvas.toDataUrl ?

推荐答案

要绘制所有三个层",您必须为所有层创建上下文

To draw on all three "layers" you must create contexts for all layers

// references to layer1
var canvas1 = document.getElementById('layer1');
var context1 = canvas1.getContext('2d');

// references to layer2
var canvas2 = document.getElementById('layer2');
var context2 = canvas2.getContext('2d');

// references to layer3
var canvas3 = document.getElementById('layer3');
var context3 = canvas3.getContext('2d');

然后在你完成所有图层的绘制之后,将它们合并(这里合并到 layer1):

Then after you’re done drawing on all layers, merge them (here merged onto layer1):

// merge all layers onto layer1
context1.drawImage(canvas2,0,0);
context1.drawImage(canvas3,0,0);

最后将合并后的画布保存到一张图片,并将您的 img src 设置为该图片.

And finally save the merged canvas to an image and set your img src to that image.

// save the merged drawings as an image
// and set the img element src to that merged image
var img=new Image();
img.onload=function(){
    document.getElementById("results").src=img.src;
}
img.src=canvas1.toDataURL();

这是代码和小提琴:http://jsfiddle.net/m1erickson/fhjwY/

<!doctype html>
<html>
<head>
<link rel="stylesheet" type="text/css" media="all" href="css/reset.css" /> <!-- reset css -->
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>

<style>
    body{ background-color: ivory; }
    canvas{border:1px solid red;}
</style>

<script>
$(function(){

// references to layer1
var canvas1 = document.getElementById('layer1');
var context1 = canvas1.getContext('2d');

// references to layer2
var canvas2 = document.getElementById('layer2');
var context2 = canvas2.getContext('2d');

// references to layer3
var canvas3 = document.getElementById('layer3');
var context3 = canvas3.getContext('2d');

// draw stuff on the layers
context1.fillStyle="red";
context2.fillStyle="blue";
context3.fillStyle="green";

context1.fillRect(20,20,50,50);
context2.fillRect(50,50,50,50);
context3.fillRect(80,80,50,50);

// merge all layers onto layer1
context1.drawImage(canvas2,0,0);
context1.drawImage(canvas3,0,0);

// save the merged drawings as an image
// and set the img element src to that merged image
var img=new Image();
img.onload=function(){
    document.getElementById("results").src=img.src;
}
img.src=canvas1.toDataURL();



}); // end $(function(){});
</script>

</head>

<body>
    <p>Draw rects on 3 canvases</p>
    <p>Merge all drawings to 1st canvas</p>
    <p>Create an image from merged drawings</p>
    <p>Set the image element with that merged image</p>
    <canvas id="layer1" width=150 height=150></canvas>
    <canvas id="layer2" width=150 height=150></canvas><br>
    <canvas id="layer3" width=150 height=150></canvas>
    <img id="results" width=150 height=150>
</body>
</html>

这篇关于在 3 层画布上绘制图像并保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Update another component when Formik form changes(当Formik表单更改时更新另一个组件)
Formik validation isSubmitting / isValidating not getting set to true(Formik验证正在提交/isValiating未设置为True)
React Validation Max Range Using Formik(使用Formik的Reaction验证最大范围)
Validation using Yup to check string or number length(使用YUP检查字符串或数字长度的验证)
Updating initialValues prop on Formik Form does not update input value(更新Formik表单上的初始值属性不会更新输入值)
password validation with yup and formik(使用YUP和Formick进行密码验证)