It used the canvas to draw the curves in the old project, and the client felt that it was vague, so I tried to make a demo about canvas, canvas optimization and svg comparison.

The effect is as follows:

<!DOCTYPE html>
<html> <head>
<style>
html,
body {
width: 100%;
height: 100%;
overflow: hidden;
}
</style>
</head> <body style="display: flex;
align-items: center;
justify-content: space-around;">
<div>
<div style=" text-align: center;
font-family: cursive;">正常 Canvas</div>
<div style="background-color:cornflowerblue;width:200px;height:200px;">
<canvas id="normalCanvas" width="200" height="200" />
</div>
</div>
<div>
<div style=" text-align: center;
font-family: cursive;">优化后 Canvas</div>
<div style="background-color:cornflowerblue;width:200px;height:200px;">
<canvas id="optimizedCanvas" style="width:200px;height:200px" width="400" height="400" />
</div>
</div>
<div>
<div style=" text-align: center;
font-family: cursive;">Svg</div>
<div id="svgDiv" style="background-color:cornflowerblue;width:200px;height:200px;">
<svg style="width:100%;height:100%;">
<circle cx="30" cy="50" r="25" />
</svg>
</div>
</div>
</body>
<script>
drawNormalCanvas();
drawOptimizedCanvas();
drawSvg(); function drawNormalCanvas() {
let canvas = document.getElementById("normalCanvas");
let context = canvas.getContext('2d');
let centerX = canvas.width / 2;
let centerY = canvas.height / 2;
let rad = Math.PI * 2 / 100;
let endNum = 0.5 * 100;
let scale = 1; context.imageSmoothingEnabled = true; context.clearRect(0, 0, canvas.width, canvas.height);
context.save();
context.strokeStyle = "#53FFFF"; //设置描边样式
context.lineWidth = 4 * scale; //设置线宽
context.beginPath(); //路径开始
context.arc(centerX, centerY, 75 * scale, -Math.PI / 2, -Math.PI / 2 + endNum * rad, false); //用于绘制圆弧context.arc(x坐标,y坐标,半径,起始角度,终止角度,顺时针/逆时针)
context.stroke(); //绘制
context.closePath(); //路径结束
context.restore();
context.save();
context.beginPath();
context.strokeStyle = "white";
context.lineWidth = 2 * scale;
context.arc(centerX, centerY, 75 * scale, 0, Math.PI * 2, false);
context.stroke();
context.closePath();
context.restore();
context.save(); //save和restore可以保证样式属性只运用于该段canvas元素
context.strokeStyle = "#fff"; //设置描边样式
//绘制字体,并且指定位置
context.fillStyle = "#fff";
context.font = "normal normal lighter 12px Microsoft YaHei"; //设置字体大小和字体
context.fillText("击败了全部用户", centerX - 40 * scale, centerY - 20 * scale);
context.font = "normal normal normal 30px arial";
context.fillText(endNum.toFixed(0) + "%", centerX - 23 * scale, centerY + 15 * scale);
context.font = "normal normal lighter 14px Microsoft YaHei"; //设置字体大小和字体
context.fillText('中级' + '', centerX - 15 * scale, centerY + 40 * scale);
context.stroke(); //执行绘制
context.restore();
} function drawOptimizedCanvas() {
let canvas = document.getElementById("optimizedCanvas");
let context = canvas.getContext('2d');
let centerX = canvas.width / 2;
let centerY = canvas.height / 2;
let rad = Math.PI * 2 / 100;
let endNum = 0.5 * 100;
let scale = 2; context.imageSmoothingEnabled = true; context.clearRect(0, 0, canvas.width, canvas.height);
context.save();
context.strokeStyle = "#53FFFF"; //设置描边样式
context.lineWidth = 4 * scale; //设置线宽
context.beginPath(); //路径开始
context.arc(centerX, centerY, 75 * scale, -Math.PI / 2, -Math.PI / 2 + endNum * rad, false); //用于绘制圆弧context.arc(x坐标,y坐标,半径,起始角度,终止角度,顺时针/逆时针)
context.stroke(); //绘制
context.closePath(); //路径结束
context.restore();
context.save();
context.beginPath();
context.strokeStyle = "white";
context.lineWidth = 2 * scale;
context.arc(centerX, centerY, 75 * scale, 0, Math.PI * 2, false);
context.stroke();
context.closePath();
context.restore();
context.save(); //save和restore可以保证样式属性只运用于该段canvas元素
context.strokeStyle = "#fff"; //设置描边样式
//绘制字体,并且指定位置
context.fillStyle = "#fff";
context.font = "normal normal lighter 24px Microsoft YaHei"; //设置字体大小和字体
context.fillText("击败了全部用户", centerX - 40 * scale, centerY - 20 * scale);
context.font = "normal normal normal 60px arial";
context.fillText(endNum.toFixed(0) + "%", centerX - 23 * scale, centerY + 15 * scale);
context.font = "normal normal lighter 28px Microsoft YaHei"; //设置字体大小和字体
context.fillText('中级' + '', centerX - 15 * scale, centerY + 40 * scale);
context.stroke(); //执行绘制
context.restore();
} function drawSvg() {
let svgDiv = document.getElementById("svgDiv");
let centerX = svgDiv.offsetWidth / 2;
let centerY = svgDiv.offsetHeight / 2;
let newHtml = `<svg style="width:100%;height:100%;">
<circle cx="${centerX}" cy="${centerY}" r="75" style="fill:none;stroke:white;stroke-width: 2px" />
<path d="M 100 25 A 75 75 0 1 1 100 175" stroke="#53FFFF" stroke-width="4px" fill="none" />
<text x="${centerX - 40}" y="${centerY - 20}" style="fill: #fff;font:normal normal lighter 12px Microsoft YaHei;">击败了全部用户</text>
<text x="${centerX - 23}" y="${centerY + 15}" style="fill: #fff;font:normal normal normal 30px arial;">50%</text>
<text x="${centerX - 15}" y="${centerY + 40}" style="fill: #fff;font:normal normal lighter 12px Microsoft YaHei;">中级</text>
</svg>
`;
svgDiv.innerHTML = newHtml;
}
</script> </html>

references:

https://echarts.apache.org/handbook/zh/best-practices/canvas-vs-svg/#

https://www.cnblogs.com/fireyjy/p/5789376.html

https://www.cnblogs.com/heibaiqi/p/16547624.html

Demo of canvas, canvas optimization and svg的更多相关文章

  1. HTML5 Canvas、内联 SVG、Canvas vs. SVG

    canvas 元素用于在网页上绘制图形. 什么是 Canvas? HTML5 的 canvas 元素使用 JavaScript 在网页上绘制图像. 画布是一个矩形区域,您可以控制其每一像素. canv ...

  2. FireFox下Canvas使用图像合成绘制SVG的Bug

    本文适合适合对canvas绘制.图形学.前端可视化感兴趣的读者阅读. 楔子 所有的事情都会有一个起因.最近产品上需要做一个这样的功能:给一些图形进行染色处理.想想这还不是顺手拈来的事情,早就研究过图形 ...

  3. 做出一个SwitchButton的效果,并详细学习一下onDraw(Canvas canvas)方法的使用

    代码的灵感和原理主要来自于android自定义开关控件-SlideSwitch http://blog.csdn.net/singwhatiwanna/article/details/9254309这 ...

  4. Html5新增元素中Canvas 与内联SVG的比较!

    SVG与Canvas的区别与比较如下: svg:使用xml描述2D图形,canvas使用javascript描述2D图形. Canvas 是逐像素进行渲染的,在 canvas 中,一旦图形被绘制完成, ...

  5. 把一个base64编码的图片绘制到canvas (canvas的图片在转成dataurl)

    把一个base64编码的图片绘制到canvas 需要引入jquery. <canvas id="myCanvas" width="800" height= ...

  6. HTML5 学习总结(四)——canvas绘图、WebGL、SVG

    一.Canvas canvas是HTML5中新增一个HTML5标签与操作canvas的javascript API,它可以实现在网页中完成动态的2D与3D图像技术.<canvas> 标记和 ...

  7. HTML5新特性——HTML 5 Canvas vs. SVG

    Canvas 和 SVG 都允许您在浏览器中创建图形,但是它们在根本上是不同的. SVG SVG 是一种使用 XML 描述 2D 图形的语言. SVG 基于 XML,这意味着 SVG DOM 中的每个 ...

  8. canvas绘图、WebGL、SVG

    目录 一.Canvas 1.1.创建canvas元素 1.2.画线 1.3.绘制矩形 1.4.绘制圆弧 1.5.绘制图像 1.6.绘制文字 1.7.随机颜色与简单动画 二.WebGL 2.1.HTML ...

  9. HTML5 学习笔记(四)——canvas绘图、WebGL、SVG

    一.Canvas canvas是HTML5中新增一个HTML5标签与操作canvas的javascript API,它可以实现在网页中完成动态的2D与3D图像技术.<canvas> 标记和 ...

  10. svg转化成canvas以便生成base64位的图片

    很久前写了关于把html转成图片的一个例子,最近有出了新的问题.利用html2canvas.js文件把html转成base64位的图片是没什么问题的,但也不是绝对的,比如这时候不能碰见svg这个鬼,h ...

随机推荐

  1. 微信小程序if for

    1.控制代码的显示隐藏 1.wx:if="{{}}"判断是否需要渲染代码 <view wx:if="{{tiaojian===1}}">显示1< ...

  2. 在grafana中使用不同的数据源及插件安装

    一.postgresql作为数据源 现在PG的版本是10.5(10+) 加入datasource 时只能指定一个数据库,对于监控来说可使用默认数据库postgres! 可以方便地作趋势图以及使用tab ...

  3. 524. 通过删除字母匹配到字典里最长单词 (Medium)

    问题描述 524. 通过删除字母匹配到字典里最长单词 (Medium) 给你一个字符串 s 和一个字符串数组 dictionary ,找出并返回 dictionary 中最长的字符串,该字符串可以通过 ...

  4. nginx 与 k8s ingress 配置转发websocket

    环境 10.1.100.10:70 是后端websocket 服务 需要通过nginx 向后端转发,nginx 配置文件如下 # cat test-ue4.conf map $http_upgrade ...

  5. go语言 cmd执行命令,遇到空格或者双引号无法执行成功的解决方案

    大部分go执行cmd命令都是,我也是这样写的 package main import ( "fmt" "os/exec" ) func main() { cmd ...

  6. C#整合ActiveMQ与SpringBoot整合ActiveMQ数据间交互

    一.SpringBoot整合AvtiveMQ部分 1.引入ActiveMQ依赖 <dependency> <groupId>org.springframework.boot&l ...

  7. mybatis批量查询

    <foreach collection="list" item="item" open="(" separator=",&q ...

  8. Mac用自带软件QuickTime Player进行录屏

    ​ Mac电脑用自带软件QuickTime Player进行录屏的教程,几步就可以学会,挺简单的. 1.首先,找到并打开QuickTime Player软件.可以鼠标右键这个图标,选择"选项 ...

  9. 关于CMDB

    关于CMDB: CMDB运维管理平台是由CMDB开发团队,针对目前服务器运维.监控,批量管理提出的一个开源. 易用.实用的跨平台服务器运维管理平台. 统筹来说cmdb就是将已有的规则化运维技术规则到运 ...

  10. .net core 根据需求不同的数据有不同的颜色