Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.0k views
in Technique[技术] by (71.8m points)

canvas生成图片只有96dpi,打印需要300dpi, 请问如何修改这个信息.

canvas生成图片只有96dpi,打印需要300dpi, 请问如何修改这个信息.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

不知道这个是不是你需要的
https://developer.mozilla.org/zh-CN/docs/Web/API/Window/devicePixelRatio

<canvas id="canvas"></canvas>
<script>
      var canvas = document.getElementById("canvas");
      var ctx = canvas.getContext("2d");

      // Set display size (css pixels).
      var size = 300;
      canvas.style.width = size + "px";
      canvas.style.height = size + "px";

      // Set actual size in memory (scaled to account for extra pixel density).
      var scale = 3.125; // 300/96
      canvas.width = Math.floor(size * scale);
      canvas.height = Math.floor(size * scale);

      // Normalize coordinate system to use css pixels.
      ctx.scale(scale, scale);

      ctx.fillStyle = "#bada55";
      ctx.fillRect(10, 10, 300, 300);
      ctx.fillStyle = "#ffffff";
      ctx.font = "18px Arial";
      ctx.textAlign = "center";
      ctx.textBaseline = "middle";

      var x = size / 2;
      var y = size / 2;

      var textString = "I love MDN";
      ctx.fillText(textString, x, y);
    </script>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...