将图像加载到画布上并放大/缩小并移动它以适应并保存画布上的内容
Posted
技术标签:
【中文标题】将图像加载到画布上并放大/缩小并移动它以适应并保存画布上的内容【英文标题】:Loading an image on to a canvas and zooming in/out and move it to fit and save what is on the canvas 【发布时间】:2022-01-02 21:47:06 【问题描述】:您好,我遇到了一些将图像加载到画布的代码。加载的图像可以移动和放大和缩小。它工作得很好,但我希望能够保存出现在画布中或画布上的图像,如果我以正确的方式得到它?我该怎么办?我希望能够选择一个文件来使用,而不是在代码中指定一个。我以前从来没有用canvas做过任何事情,谢谢
我已经添加了这个新的位来保存它,但我一直收到这个错误,是因为它是从另一个来源加载的吗?
到 html 保存图片
function saveImg()
var fullQuality = canvas.toDataURL('image/jpeg', 1.0);
console.log(fullQuality);
var canvas = document.getElementsByTagName('canvas')[0];
canvas.width = 200;
canvas.height = 200;
var gkhead = new Image;
window.onload = function()
var ctx = canvas.getContext('2d');
trackTransforms(ctx);
function redraw()
// Clear the entire canvas
var p1 = ctx.transformedPoint(0,0);
var p2 = ctx.transformedPoint(canvas.width,canvas.height);
ctx.clearRect(p1.x,p1.y,p2.x-p1.x,p2.y-p1.y);
ctx.save();
ctx.setTransform(1,0,0,1,0,0);
ctx.clearRect(0,0,canvas.width,canvas.height);
ctx.restore();
ctx.drawImage(gkhead,0,0);
redraw();
var lastX=canvas.width/2, lastY=canvas.height/2;
var dragStart,dragged;
canvas.addEventListener('mousedown',function(evt)
document.body.style.mozUserSelect = document.body.style.webkitUserSelect = document.body.style.userSelect = 'none';
lastX = evt.offsetX || (evt.pageX - canvas.offsetLeft);
lastY = evt.offsetY || (evt.pageY - canvas.offsetTop);
dragStart = ctx.transformedPoint(lastX,lastY);
dragged = false;
,false);
canvas.addEventListener('mousemove',function(evt)
lastX = evt.offsetX || (evt.pageX - canvas.offsetLeft);
lastY = evt.offsetY || (evt.pageY - canvas.offsetTop);
dragged = true;
if (dragStart)
var pt = ctx.transformedPoint(lastX,lastY);
ctx.translate(pt.x-dragStart.x,pt.y-dragStart.y);
redraw();
,false);
canvas.addEventListener('mouseup',function(evt)
dragStart = null;
if (!dragged) zoom(evt.shiftKey ? -1 : 1 );
,false);
var scaleFactor = 1.1;
var zoom = function(clicks)
var pt = ctx.transformedPoint(lastX,lastY);
ctx.translate(pt.x,pt.y);
var factor = Math.pow(scaleFactor,clicks);
ctx.scale(factor,factor);
ctx.translate(-pt.x,-pt.y);
redraw();
var handleScroll = function(evt)
var delta = evt.wheelDelta ? evt.wheelDelta/40 : evt.detail ? -evt.detail : 0;
if (delta) zoom(delta);
return evt.preventDefault() && false;
;
canvas.addEventListener('DOMMouseScroll',handleScroll,false);
canvas.addEventListener('mousewheel',handleScroll,false);
;
gkhead.src = 'https://www.uberfantasies.com/img/avatar.png';
// Adds ctx.getTransform() - returns an SVGMatrix
// Adds ctx.transformedPoint(x,y) - returns an SVGPoint
function trackTransforms(ctx)
var svg = document.createElementNS("http://www.w3.org/2000/svg",'svg');
var xform = svg.createSVGMatrix();
ctx.getTransform = function() return xform; ;
var savedTransforms = [];
var save = ctx.save;
ctx.save = function()
savedTransforms.push(xform.translate(0,0));
return save.call(ctx);
;
var restore = ctx.restore;
ctx.restore = function()
xform = savedTransforms.pop();
return restore.call(ctx);
;
var scale = ctx.scale;
ctx.scale = function(sx,sy)
xform = xform.scaleNonUniform(sx,sy);
return scale.call(ctx,sx,sy);
;
var rotate = ctx.rotate;
ctx.rotate = function(radians)
xform = xform.rotate(radians*180/Math.PI);
return rotate.call(ctx,radians);
;
var translate = ctx.translate;
ctx.translate = function(dx,dy)
xform = xform.translate(dx,dy);
return translate.call(ctx,dx,dy);
;
var transform = ctx.transform;
ctx.transform = function(a,b,c,d,e,f)
var m2 = svg.createSVGMatrix();
m2.a=a; m2.b=b; m2.c=c; m2.d=d; m2.e=e; m2.f=f;
xform = xform.multiply(m2);
return transform.call(ctx,a,b,c,d,e,f);
;
var setTransform = ctx.setTransform;
ctx.setTransform = function(a,b,c,d,e,f)
xform.a = a;
xform.b = b;
xform.c = c;
xform.d = d;
xform.e = e;
xform.f = f;
return setTransform.call(ctx,a,b,c,d,e,f);
;
var pt = svg.createSVGPoint();
ctx.transformedPoint = function(x,y)
pt.x=x; pt.y=y;
return pt.matrixTransform(xform.inverse());
body background:#eee; margin:1em; text-align:center;
canvas border-radius:25%;display:block; margin:1em auto; background:#fff; border:1px solid #ccc
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p>Showing how to use transform methods on the HTML5 Canvas Context to selectively zoom in and out. Drag to pan. Click to zoom at that location. Shift-click to zoom out. Mousewheel up/down over the canvas to zoom in to/out from that location.</p>
<canvas></canvas>
【问题讨论】:
【参考方案1】:要保存画布上可见的任何内容,您可以使用画布元素的toDataURL
属性。
您可以参考this网址参考。
类似这样的:-
var dataURL = canvas.toDataURL("image/jpeg", 1.0);
注意:- dataURL 包含base64格式的图片数据。
【讨论】:
嗨,它给了我 DOMException:无法在“HTMLCanvasElement”上执行“toDataURL”:可能无法导出受污染的画布。 您的问题请参考此网址***.com/questions/22710627/… 您可以将图像的跨域属性设置为匿名。<img crossorigin="anonymous"></img>
以上是关于将图像加载到画布上并放大/缩小并移动它以适应并保存画布上的内容的主要内容,如果未能解决你的问题,请参考以下文章