简体   繁体   English

无法在 html5 画布上绘制

[英]Cannot draw on html5 canvas

I created Hi dpi canvas and then tried to draw box, but something went wrong.我创建了 Hi dpi 画布,然后尝试绘制框,但出了点问题。 Why I cannot draw on canvas, could anyone help me?为什么我不能在画布上画画,有人可以帮我吗?

No errors occured & draw function is executing but I have no result没有发生错误并且正在执行绘图功能,但我没有结果

 var HiDPICanvas = function(container_id, color, w, h) { /* canvas will be placed in the container canvas will have width w and height h */ var ratio = function() { // return pixel ratio var ctx = document.createElement("canvas").getContext("2d"); var dpr = window.devicePixelRatio || 1; var bsr = ctx.webkitBackingStorePixelRatio || ctx.mozBackingStorePixelRatio || ctx.msBackingStorePixelRatio || ctx.oBackingStorePixelRatio || ctx.backingStorePixelRatio || 1; return dpr / bsr; } var createHiDPICanvas = function() { if (!ratio) { ratio = ratio(); } var chart_container = document.getElementById(container_id); var can = document.createElement("canvas"); can.style.backgroundColor = color can.width = w * ratio; can.height = h * ratio; can.style.width = w + "px"; can.style.height = h + "px"; can.getContext("2d").setTransform(ratio, 0, 0, ratio, 0, 0); chart_container.appendChild(can); return can; } var canvas = createHiDPICanvas() return { canvas : canvas, ctx : canvas.getContext("2d"), width : w, height : h, color : color } } // cci -> canvas ctx info (dict) var cci = HiDPICanvas("lifespanChart", "bisque", 780, 640) var ctx = cci.ctx var canvas = cci.canvas var Box = function(color) { var create = function() { ctx.save(); ctx.globalCompositeOperation = "source-over"; ctx.beginPath(); ctx.lineWidth = 0.001; ctx.fillStyle = color; ctx.moveTo(-1, -1); ctx.lineTo(50, -1) ctx.lineTo(50, 50) ctx.lineTo(-1, 50) ctx.lineTo(-1, -1) console.log("drawing square") ctx.stroke(); ctx.closePath(); ctx.fill(); ctx.restore() } create() return { refresh : create, color : color } } var borders = Box("red")
 <div> <div id="lifespanChart"></div> </div>

It should draw box but it does not, what I forgot to do?它应该画框但它没有,我忘了做什么?

Added some text here because it do not want to let me ask quesiton because a lot of code, but I have nothing to describe anymore在这里添加了一些文字,因为它不想让我问问题,因为代码很多,但我已经没有什么可描述的了

Thank you in advance先感谢您

You've messed up with ratio .你搞砸了ratio It is a variable and function at the same time...它同时是一个变量和函数......

I change function name to getRatio and added variable.我将函数名称更改为getRatio并添加了变量。 Now works:现在工作:

 var HiDPICanvas = function(container_id, color, w, h) { /* canvas will be placed in the container canvas will have width w and height h */ var getRatio = function() { // return pixel ratio var ctx = document.createElement("canvas").getContext("2d"); var dpr = window.devicePixelRatio || 1; var bsr = ctx.webkitBackingStorePixelRatio || ctx.mozBackingStorePixelRatio || ctx.msBackingStorePixelRatio || ctx.oBackingStorePixelRatio || ctx.backingStorePixelRatio || 1; return dpr / bsr; } var createHiDPICanvas = function() { let ratio = getRatio(); var chart_container = document.getElementById(container_id); var can = document.createElement("canvas"); can.style.backgroundColor = color can.width = w * ratio; can.height = h * ratio; can.style.width = w + "px"; can.style.height = h + "px"; can.getContext("2d").setTransform(ratio, 0, 0, ratio, 0, 0); chart_container.appendChild(can); return can; } var canvas = createHiDPICanvas() return { canvas : canvas, ctx : canvas.getContext("2d"), width : w, height : h, color : color } } // cci -> canvas ctx info (dict) var cci = HiDPICanvas("lifespanChart", "bisque", 780, 640) var ctx = cci.ctx var canvas = cci.canvas var Box = function(color) { var create = function() { ctx.save(); ctx.globalCompositeOperation = "source-over"; ctx.beginPath(); ctx.lineWidth = 0.001; ctx.fillStyle = color; ctx.moveTo(-1, -1); ctx.lineTo(50, -1) ctx.lineTo(50, 50) ctx.lineTo(-1, 50) ctx.lineTo(-1, -1) console.log("drawing square") ctx.stroke(); ctx.closePath(); ctx.fill(); ctx.restore() } create() return { refresh : create, color : color } } var borders = Box("red")
 <div> <div id="lifespanChart"></div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM