【发布时间】:2015-07-11 01:01:03
【问题描述】:
我需要一些帮助:
我正在尝试从我的 html 画布中保存图像。我确实让它工作了,但是当我点击“保存”按钮时,我只得到了我生成的彩色背景。我在画布中使用图像(png 图像),当我尝试将画布保存到文件时,它们似乎消失了。
奇怪的是:当我右键单击并选择“保存图像”时,它似乎工作正常!我应该在我的编码中保留一些东西,还是这不可能?
我在互联网上寻找解决方案,但没有找到适合我的解决方案。我对 html 画布也很陌生。任何帮助将不胜感激!
代码:
<style>
#buttonRows{width:100%; height:200px;float:left;background-color:#666;}
#buttonRow_01{width:100%; height:40px; border-top:1px solid #333; float:left;margin-top:10px;}
#buttonRow_02{width:100%; height:40px; border-top:1px solid #333; float:left;margin-top:10px;}
#label{padding:10px;float:left;height:50px;}
#button{width:300px;height:50px; background-color:#CCC;border:none;cursor:pointer;}
#img_container{ width:500px; margin:20px; border:5px solid #000;}
#img_container img{ width:100%;height:auto}
</style>
<div id="buttonRows">
<input id="button" type="button" value="Update button" onclick="refresh();" />
<div id="buttonRow_01">
<div id="label">Achtergrondkleur:</div>
<input type="textbox" value="FF0000" id="colorInput" class="color" onchange="refresh();"/>
</div>
<div id="buttonRow_02">
<div id="label">Laag 1:</div>
<input type="range" value="50" id="opacitySlider"/>
<select id="blendSelect" >
<option value="screen">screen</option>
<option value="multiply">multiply</option>
<option value="source-over">none</option>
</select>
<select id="verbandSelect" >
<option value="bs_standaart_cropped.png">standaart</option>
<option value="bs_wild_cropped.png">wild</option>
</select>
</div>
</div>
<canvas id="myCanvas" width="1280" height="720" ></canvas>
<img id="canvasImg" src="" />
<script>
function refresh(){
var canvas = document.getElementById('myCanvas');
var context = canvas.getContext('2d');
context.clearRect(0, 0, canvas.width, canvas.height);
context.globalAlpha = 1;
context.globalCompositeOperation = "source-over";
var CI = document.getElementById('colorInput');
var OS = document.getElementById('opacitySlider');
var BS = document.getElementById('blendSelect');
var VS = document.getElementById('verbandSelect');
// draw blue rectangle
context.beginPath();
context.rect(0, 0, 1280, 720);
context.fillStyle = '#' + CI.value;
context.fill();
// draw transparent red circle
context.globalAlpha = OS.value/100;
context.globalCompositeOperation = BS.value;
var image = new Image();
image.src = "http://www.joey-cooijmans.nl/templates/JC-main/images/steenconf/" + VS.value;
image.fillStyle="#00ff00";
context.drawImage(image, 0, 0, canvas.width, canvas.height);
}
refresh();
</script>
【问题讨论】:
-
您可以将代码添加到帖子中,以便我们知道您在说什么。
-
很遗憾,没有发布您的代码 - 这个问题太宽泛,无法回答...发布代码,以便我们提供帮助!
-
哦,对了,对不起,我忘了! (我在网站上的第一篇文章:P)
-
图像没有填充样式 (
image.fillStyle="#00ff00";),此外,您缺少图像的加载处理程序。如果您绘制的图像不符合 CORS 要求,则您的画布将被“污染”并且不允许您将其提取为图像。控制台说什么? -
大家好!我自己找到了一个解决方案:它是关于图像的路径。我将路径更改为相对路径,现在 ik 就像一个魅力!感谢您的帮助
标签: javascript html canvas html5-canvas