我将假定这是某种兼容性问题。一切都可以在chrome中完美运行,但是在Firefox中,它根本无法画任何东西。
function drawStage(stageNum) { var cap = canvasArray.length; //keeps the canvasElements var i; var stageImages = images["stage" + stageNum]; var stageDimensions = imageDimensions["stage" + stageNum]; //console.log("Cap is: " + cap); for (i = 0; i < cap; i++) { var canvas = document.getElementById(canvasArray[i]); var canvasContext = canvas.getContext("2d"); var image = document.getElementById(stageImages[i]); canvasContext.clearRect(0, 0, 1280, 1280); canvasContext.drawImage(image, stageDimensions[i][0], stageDimensions[i][1], stageDimensions[i][2], stageDimensions[i][3]); //document.getElementById(zIndexes[i][0]).style["z-index"] = zIndexes[i][stageNum]; //console.log(document.getElementById(zIndexes[i][0]).id); } }
编辑: 这是我制作的“测试”代码示例的保管箱链接,它更简单,1张图像,1个画布,1个div,相似(实际上是相同的)脚本,但仍无法在Firefox中使用,但可以在chrome中使用。如果您可以在那里解决问题,那么这也将得到解决。
更新:数组在下面,根据请求,它们是不同元素/坐标的简单输入。不过,我向您保证,数组本身没有问题,在chrome中一切正常。该函数中有关DrawImage()方法的某些问题引起了我要说的问题。(数组下方的进一步说明)
images是元素的(全局)id数组。
var images = { stage1: ["character1Base", "character2Base", "character3Base", "character4Base", "character5Base", "character6Base", "character3BotImg"], stage2: ["character1Sit", "character2Base", "character3Base", "character4Base", "character5Base", "character6Base", "character3BotImg"], stage3: ["character1Sit", "character2Drink", "character3Base", "character4Base", "character5Base", "character6Base", "character3BotImg"], stage4: ["character1Sit", "character2Drink", "character3Base", "character4Base", "character5Base", "character6Doll", "character3BotImg"], stage5: ["character1Sit", "character2Drink", "character3Eat", "character4Base", "character5Base", "character6Doll", "character3EatBot"], stage6: ["character1Sit", "character2Stand", "character3Eat", "character4Base", "character5Base", "character6Doll", "character3EatBot"], stage7: ["character1Sit", "character2Stand", "character3Eat", "character4Sit", "character5Base", "character6Doll", "character3EatBot"], stage8: ["character1Sit", "character2Stand", "character3Eat", "character4Sit", "character5Sit", "character6Doll", "character3EatBot"], stage9: ["character1Sit", "character2Eat", "character3Eat", "character4Sit", "character5Sit", "character6Doll", "character3EatBot"], stage10: ["character1Drink", "character2Eat", "character3Eat", "character4Sit", "character5Sit", "character6Doll", "character3EatBot"] };
imageDimensions是井,尺寸,posX,posY,sizeX和sizeY的(全局)数组。
var imageDimensions = { stage1: [[0, 0, 233, 485], [0, 0, 153, 407], [20, 0, 220, 200], [0, 150, 505, 210], [0, 0, 315, 391], [0, 0, 480, 435], [1, 0, 220, 180]], stage2: [[105, 35, 180, 440], [0, 0, 153, 407], [20, 0, 220, 200], [0, 150, 505, 210], [0, 0, 315, 391], [0, 0, 480, 435], [1, 0, 220, 180]], stage3: [[105, 35, 180, 440], [0, 0, 153, 407], [20, 0, 220, 200], [0, 150, 505, 210], [0, 0, 315, 391], [0, 0, 480, 435], [1, 0, 220, 180]], stage4: [[105, 35, 180, 440], [0, 0, 153, 407], [20, 0, 220, 200], [0, 150, 505, 210], [0, 0, 315, 391], [0, 0, 480, 435], [1, 0, 220, 180]], stage5: [[105, 35, 180, 440], [0, 0, 153, 407], [3, 7, 226, 180], [0, 150, 505, 210], [0, 0, 315, 391], [0, 0, 480, 435], [1, 9, 220, 180]], stage6: [[105, 35, 180, 440], [0, 0, 172, 490], [3, 7, 226, 180], [0, 150, 505, 210], [0, 0, 315, 391], [0, 0, 480, 435], [1, 9, 220, 180]], stage7: [[105, 35, 180, 440], [0, 0, 172, 490], [3, 7, 226, 180], [70, 0, 341, 380], [0, 0, 315, 391], [0, 0, 480, 435], [1, 9, 220, 180]], stage8: [[105, 35, 180, 440], [0, 0, 172, 490], [3, 7, 226, 180], [70, 0, 341, 380], [18, 72, 305, 422], [0, 0, 480, 435], [1, 9, 220, 180]], stage9: [[105, 35, 180, 440], [0, 0, 173, 473], [3, 7, 226, 180], [70, 0, 341, 380], [18, 72, 305, 422], [0, 0, 480, 435], [1, 9, 220, 180]], stage10: [[105, 32, 162, 440], [0, 0, 173, 473], [3, 7, 226, 180], [70, 0, 341, 380], [18, 72, 305, 422], [0, 0, 480, 435], [1, 9, 220, 180]] };
说明:
该功能旨在在屏幕上绘制不同的元素,这些元素属于这些元素。它们是在画布中绘制的(稍后还将在画布中进行动画处理)。该stageNum
参数是函数采用的唯一参数,它决定要绘制的内容。
有人知道是什么问题吗?(我读过一些类似的文章,但所有问题都是鼠标位置的layerX,layerY,但是我根本不在这里使用mouseover / mousein / mouseout,仍然没有绘制元素。
您的问题是您要绘制的图像是svg图像,并且这些svg文档具有相对width
和height
属性。
浏览器无法为其要绘制的图像设置高度或宽度,因此无法将其呈现到画布上。(它可以在文档中进行估算,因为它可以相对于某物,但不能相对于画布)。
因此,解决方案是在svg文件中设置绝对值width
和height
属性,
或者,更复杂的是,首先将其绘制为或
,然后绘制一个序列化版本,在该版本中将设置这些属性。
function initialize() { var canvas = document.getElementById("char1Canvas"); var canvasContext = canvas.getContext("2d"); var image = document.getElementById("char1Img"); resizeSVG(image, function(e){ canvasContext.clearRect(0, 0, 1280, 1280); canvasContext.drawImage(this, 0, 0); }); }; var resizeSVG = function(svgImg, callback){ // create an iframe var iframe = document.createElement('iframe'); // so we don't see it iframe.height = 0; iframe.width = 0; iframe.onload = function(){ var doc = iframe.contentDocument; var svg = doc.querySelector('svg'); // get the computed width and height of your img element // should probably be tweaked var bbox = svgImg.getBoundingClientRect(); // if it's a relative width if (svg.width.baseVal.unitType !== 1) { svg.setAttribute('width', bbox.width); } // or a relative height if (svg.height.baseVal.unitType !== 1) { svg.setAttribute('height', bbox.height); } // serialize our updated svg var svgData = (new XMLSerializer()).serializeToString(svg); var svgURL = 'data:image/svg+xml; charset=utf8, ' + encodeURIComponent(svgData); // create a new Image Object that ill be draw on the canvas var img = new Image(); img.onload = callback; img.src = svgURL; // remove the iframe document.body.removeChild(iframe); }; iframe.src = svgImg.src; document.body.appendChild(iframe); }