Javascript 如何将SVG文件添加到具有动态维度的HTML画布元素中?
我的网络游戏有问题。我尝试将一个已编码到数据URL中的SVG绘制到画布中,该画布应具有与文档窗口相同的维度,即使在重新缩放实际窗口时也是如此Javascript 如何将SVG文件添加到具有动态维度的HTML画布元素中?,javascript,html,css,svg,html5-canvas,Javascript,Html,Css,Svg,Html5 Canvas,我的网络游戏有问题。我尝试将一个已编码到数据URL中的SVG绘制到画布中,该画布应具有与文档窗口相同的维度,即使在重新缩放实际窗口时也是如此 (函数(){ var canvas=document.getElementById('canvas'), context=canvas.getContext('2d'); //调整画布大小以动态填充浏览器窗口 addEventListener('resize',resizeCanvas,false); 函数resizeCanvas(){ canvas.w
(函数(){
var canvas=document.getElementById('canvas'),
context=canvas.getContext('2d');
//调整画布大小以动态填充浏览器窗口
addEventListener('resize',resizeCanvas,false);
函数resizeCanvas(){
canvas.width=window.innerWidth;
canvas.height=window.innerHeight;
/**
*您的图形需要位于该功能内,否则将在
*调整浏览器窗口的大小,画布将被清除。
*/
drawSVGBackground();
}
调整画布的大小();
函数drawSVGBackground(){
var img=新图像();
img.onload=函数(){
ctx.drawImage(img,0,0);
};
img.src=“data:image/svg+xml;base64(一个非常长的加密,占用了很多空间)”;
}
})();代码>
*{
保证金:0;
填充:0;
}
/*删除顶部和左侧空白的步骤*/
html,
身体{
宽度:100%;
身高:100%;
}
/*只是为了确保这些都是全屏的*/
帆布{
显示:块;
}
/*要删除滚动条*/
首先
第二,
第三,我重构/修复了您的JavaScript:
(function() {
var canvas = document.getElementById('canvas'),
context = canvas.getContext('2d'),
img = new Image();
// resize the canvas to fill browser window dynamically
window.addEventListener('resize', resizeCanvas, false);
function resizeCanvas() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
/**
* Your drawings need to be inside this function otherwise they will be reset when
* you resize the browser window and the canvas goes will be cleared.
*/
drawSVGBackground();
}
resizeCanvas();
function drawSVGBackground() {
context.drawImage(img, 0, 0, canvas.width, canvas.height);
}
img.onload = function() {
drawSVGBackground();
};
img.src = "data:image/svg+xml;base64,(A really long encryption that took up a lot of space)";
})();
首先
第二,
第三,我重构/修复了您的JavaScript:
(function() {
var canvas = document.getElementById('canvas'),
context = canvas.getContext('2d'),
img = new Image();
// resize the canvas to fill browser window dynamically
window.addEventListener('resize', resizeCanvas, false);
function resizeCanvas() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
/**
* Your drawings need to be inside this function otherwise they will be reset when
* you resize the browser window and the canvas goes will be cleared.
*/
drawSVGBackground();
}
resizeCanvas();
function drawSVGBackground() {
context.drawImage(img, 0, 0, canvas.width, canvas.height);
}
img.onload = function() {
drawSVGBackground();
};
img.src = "data:image/svg+xml;base64,(A really long encryption that took up a lot of space)";
})();
您可能从不同的源复制粘贴的代码,因此变量名不一致
context = canvas.getContext('2d');
被定义为
但是,您稍后将上下文称为:
ctx.drawImage(img, 0, 0);
未定义ctx
尝试:
您可能从不同的源复制粘贴的代码,因此变量名不一致
context = canvas.getContext('2d');
被定义为
但是,您稍后将上下文称为:
ctx.drawImage(img, 0, 0);
未定义ctx
尝试:
旁注:上次我检查时,firefox没有在画布中正确缩放SVG。也许这是固定的,请确保使用多个浏览器进行测试。@JacqueGoupil,width和height属性应设置为绝对长度(即no%),否则FF根本不会绘制它,实际上只有chrome会尝试进行猜测。否则,我从未见过FF在与drawImage()
Sidenote()一起使用时不能正确缩放svg。注意:上次我检查时,firefox在画布中不能正确缩放svg。也许这是固定的,请确保使用多个浏览器进行测试。@JacqueGoupil,width和height属性应设置为绝对长度(即no%),否则FF根本不会绘制它,实际上只有chrome会尝试进行猜测。否则,我从未见过FF在与drawImage()
Thank you Thank you Thank you Thank you Thank you Thank you Thank you Thank you Thank you Thank you-