Canvas is great for high performance graphics rendering but by default the results look blocky on phones tablets and laptops with high pixel density or Retina displays. By using canvas width and height attributes and style props we can use window.devicePixelRatio to create a canvas that is responsive to pixel ratio.

It's much faster to paint graphics to the screen using canvas and DOM. But on high pixel density displays, like phones and Macbooks, the results look pixelated. This is because by default one canvas pixel equals one CSS pixel, not one screen pixel.

To fix this, let's create a scale factor from the device pixel ratio. We use a scale factors to create a canvas, use width and height attributes, so double the CSS pixel width and height sizes we require. We make all our painting calls relative to the scale factor.

const canvas = document.querySelector('canvas')
const sf = window.devicePixelRatio
const elWidth = canvas.width
const elHeight = canvas.height
canvas.width = elWidth * sf
canvas.height = elHeight * sf
const ctx = canvas.getContext('2d')
ctx.arc(
canvas.width / 2,
canvas.height / 2,
canvas.width / 2,
0,
Math.PI * 2
)
ctx.fill()
canvas.style.width = `${elWidth}px`

To recap, the width and height attributes on the canvas element determine the number of pixels in the canvas, while the CSS width and height properties determine the size it will display on the screen. By setting an element's attributes different to the CSS properties.

[Canvas] Make Canvas Responsive to Pixel Ratio的更多相关文章

  1. canvas 利用canvas中的globalCompositeOperation 绘制刮奖 效果

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <t ...

  2. [canvas]用canvas绘制饼状图

    折线图之后又来饼状图啦~\(≧▽≦)/~啦啦啦 <!DOCTYPE html> <html lang="en"> <head> <meta ...

  3. [canvas]利用canvas绘制自适应的折线图

    前段时间学习了用canvas绘制折现图,且当画布变换大小,折现图会随之变化,现附上代码 <!DOCTYPE html> <html lang="en"> & ...

  4. [ html canvas putImageData ] canvas绘图属性 putImageData 属性讲解

    <!DOCTYPE html> <html lang='zh-cn'> <head> <title>Insert you title</title ...

  5. [ html canvas globalCompositeOperation ] canvas绘图属性 设置合成图像如何显示 属性演示

    <!DOCTYPE html> <html lang='zh-cn'> <head> <title>Insert you title</title ...

  6. Canvas名侦探柯南-canvas练习

    var canvas=document.getElementById("canvas"); var ctx=canvas.getContext("2d"); / ...

  7. Device Pixel Ratio & Media Queries

    一些重要的名词解释: CSS pixels(CSS 像素):详见http://www.w3.org/TR/css3-values/#reference-pixe CSS声明的像素值,可随着放大缩小而放 ...

  8. 移动端 解决自适应 和 多种dpr (device pixel ratio) 的 [淘宝] 解决方案 lib-flexible

    其实H5适配的方案有很多种,网上有关于这方面的教程也非常的多. 不管哪种方法,都有其自己的优势和劣势. 为什么推荐使用Flexible库来做H5页面的终端设备适配呢?   原理  简单易懂  源码疑问 ...

  9. dpr——设备像素比(device pixel ratio)

    设备像素比 = 物理像素 / 逻辑像素 1.物理像素 显示器上最小的物理显示单元(像素颗粒),在操作系统的调度下,每一个设备像素都有自己的颜色值和亮度值. 例如:手机大小固定,物理像素越高,画面越清晰 ...

随机推荐

  1. order by调优的一些测试

    表结构信息:mysql> show create table tb\G*************************** 1. row *************************** ...

  2. POJ 2362 Square

    题意:给n个木棍,问能不能正好拼成一个正方形. 解法:POJ1011的简单版……不需要太多剪枝……随便剪一剪就好了……但是各种写屎来着QAQ 代码: #include<stdio.h> # ...

  3. Delphi word 颜色

    unit Unit1;   interface   uses   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, ...

  4. 【LeetCode 231】Power of Two

    Given an integer, write a function to determine if it is a power of two. 思路: 如果一个数是2的Power,那么该数的二进制串 ...

  5. Ye.云狐J2刷机笔记 | 完美切换内部存储卡和SD卡的改法.vold.fstab

    ================================================================================Ye.完美切换内部存储卡和SD卡成功.v ...

  6. rman的conver方法拷贝ASM文件

    rman中的conver命令主要用户跨平台传输表空间,也可以完成从ASM何本地文件系统中拷贝文件,比用dbms_file_transfer方法要简单 从ASM拷贝到文件系统: 拷贝表空间 在拷贝表空间 ...

  7. PhoneGap,Cordova[3.5.0-0.2.6]:利用插件Cordova-SQLitePlugin来操作SQLite数据库

    在PhoneGap应用程序中,我们可以利用一款名叫Cordova-SQLitePlugin的插件来方便的操作基于浏览器内置数据库或独立的SQLite数据库文件,此插件的基本信息: 1.项目地址:htt ...

  8. xmpp 协议之可扩展消息(messaging)与状态(presence)协议核心: RFC 3920

    附:asmack已经不需要了,4.1以后的smack已经可以在android上使用了. XMPP Clients:   http://xmpp.org/software/clients.html An ...

  9. 【转】Nginx系列(五)--nginx+tomcat实现负载均衡

    原博文出于:  http://blog.csdn.net/liutengteng130/article/details/47129909   感谢! Nginx占有内存少,并发能力强,事实上Nginx ...

  10. 设置结点的ID为固定ID

    https://www.java.net//forum/topic/jxta/jxta-community-forum/how-initialize-pse-jxse-27 ————————————— ...