[Canvas] Make Canvas Responsive to Pixel Ratio
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的更多相关文章
- canvas 利用canvas中的globalCompositeOperation 绘制刮奖 效果
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <t ...
- [canvas]用canvas绘制饼状图
折线图之后又来饼状图啦~\(≧▽≦)/~啦啦啦 <!DOCTYPE html> <html lang="en"> <head> <meta ...
- [canvas]利用canvas绘制自适应的折线图
前段时间学习了用canvas绘制折现图,且当画布变换大小,折现图会随之变化,现附上代码 <!DOCTYPE html> <html lang="en"> & ...
- [ html canvas putImageData ] canvas绘图属性 putImageData 属性讲解
<!DOCTYPE html> <html lang='zh-cn'> <head> <title>Insert you title</title ...
- [ html canvas globalCompositeOperation ] canvas绘图属性 设置合成图像如何显示 属性演示
<!DOCTYPE html> <html lang='zh-cn'> <head> <title>Insert you title</title ...
- Canvas名侦探柯南-canvas练习
var canvas=document.getElementById("canvas"); var ctx=canvas.getContext("2d"); / ...
- Device Pixel Ratio & Media Queries
一些重要的名词解释: CSS pixels(CSS 像素):详见http://www.w3.org/TR/css3-values/#reference-pixe CSS声明的像素值,可随着放大缩小而放 ...
- 移动端 解决自适应 和 多种dpr (device pixel ratio) 的 [淘宝] 解决方案 lib-flexible
其实H5适配的方案有很多种,网上有关于这方面的教程也非常的多. 不管哪种方法,都有其自己的优势和劣势. 为什么推荐使用Flexible库来做H5页面的终端设备适配呢? 原理 简单易懂 源码疑问 ...
- dpr——设备像素比(device pixel ratio)
设备像素比 = 物理像素 / 逻辑像素 1.物理像素 显示器上最小的物理显示单元(像素颗粒),在操作系统的调度下,每一个设备像素都有自己的颜色值和亮度值. 例如:手机大小固定,物理像素越高,画面越清晰 ...
随机推荐
- setFocusable、setEnabled、setClickable区别
setClickable 设置为true时,表明控件可以点击,如果为false,就不能点击:“点击”适用于鼠标.键盘按键.遥控器等:注意,setOnClickListener方法会默认把控件的set ...
- Leetcode Largest Number c++ solution
Total Accepted: 16020 Total Submissions: 103330 Given a list of non negative integers, arrange t ...
- 多校6 1010 HDU5802 Windows 10 dfs
// 多校6 1010 HDU5802 Windows 10 // 题意:从p到q有三种操作,要么往上升只能1步,要么往下降,如果连续往下降就是2^n, // 中途停顿或者向上,下次再降的时候从1开始 ...
- Google Appengine参考路径
1.Hello, World! in 5 minutes 2.Creating a Guestbook -Introduction 3.Sample Applications 1.Programmin ...
- 【移动开发】安卓Lab2(02)
Design UI 总共有三个activity 1. MainActitivty: search bar:先用edittext来代替 map:用imageview装图片 2. DetailActiti ...
- HDU-4751 Divide Groups 染色问题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4751 题意:有n个人,每个人都认识一些人,要求把他们分成两个集合,使得两个集合中的人都相符两两认识. ...
- J2EE到底是什么
目前所有的B/S系统应用可以分为:有状态(statefull)和无状态(stateless)两大类别. 有状态是指在整个系统的处理过程中要保留记住一些信息,而无状态则相反,每次request都是独立的 ...
- 第二百八十五天 how can I 坚持
今天好平凡啊. 晚上给徐斌打电话说忘带钥匙了,一块吃了个饭. 回到家,什么都不想做,好消沉. 玩了几局象棋,很多东西只是玩玩,但还是会认真,认真就会输,好惨. 最近在关注万科幸福里,可是.首付付不起啊 ...
- Base64编解码(C++版)
#include <string> using namespace std; class ZBase64 { public: /*编码 DataByte [ ...
- 06 java中常量以及常量池
1.举例说明 变量 常量 字面量 int a=10; float b=1.234f; String c="abc"; final long d=10L; a,b,c为变量,d为常量 ...