angular的例子:

<!DOCTYPE html>
<html ng-app="APP">
<head>
<meta charset="UTF-8">
<script src="http://cdn.bootcss.com/angular.js/1.3.0-beta.12/angular.min.js"></script> </head>
<body ng-controller="MainCtrl">
<!--
界面的这个元素会被替换成canvas元素;
-->
<div ang:round:progress data-round-progress-model="roundProgressData"></div>
<br>
<input type="number" ng-model="roundProgressData.label"/>
<script>
//引用angular.directives-round-progress这个模块;
var APP = angular.module('APP', ['angular.directives-round-progress']).
controller('MainCtrl', function($scope) {
$scope.roundProgressData = {
//这个是初始化的数据;
label: 11,
percentage: 0.11
} //通过监听scope下的这个roundProgressData属性, 对界面的canvas进行重绘;
$scope.$watch('roundProgressData', function (newValue) {
newValue.percentage = newValue.label / 100;
}, true);
});
</script> <script>
/*!
* AngularJS Round Progress Directive
*
* Copyright 2013 Stephane Begaudeau
* Released under the MIT license
*/
angular.module('angular.directives-round-progress', []).directive('angRoundProgress', [function () {
var compilationFunction = function (templateElement, templateAttributes, transclude) {
if (templateElement.length === 1) {
//初始化DOM模型, 包括初始化canvas等;
var node = templateElement[0]; var width = node.getAttribute('data-round-progress-width') || '400';
var height = node.getAttribute('data-round-progress-height') || '400'; var canvas = document.createElement('canvas');
canvas.setAttribute('width', width);
canvas.setAttribute('height', height);
canvas.setAttribute('data-round-progress-model', node.getAttribute('data-round-progress-model')); //相当于demo, 替换原来的元素;
node.parentNode.replaceChild(canvas, node); //各种配置;
var outerCircleWidth = node.getAttribute('data-round-progress-outer-circle-width') || '20';
var innerCircleWidth = node.getAttribute('data-round-progress-inner-circle-width') || '5'; var outerCircleBackgroundColor = node.getAttribute('data-round-progress-outer-circle-background-color') || '#505769';
var outerCircleForegroundColor = node.getAttribute('data-round-progress-outer-circle-foreground-color') || '#12eeb9';
var innerCircleColor = node.getAttribute('data-round-progress-inner-circle-color') || '#505769';
var labelColor = node.getAttribute('data-round-progress-label-color') || '#12eeb9'; var outerCircleRadius = node.getAttribute('data-round-progress-outer-circle-radius') || '100';
var innerCircleRadius = node.getAttribute('data-round-progress-inner-circle-radius') || '70'; var labelFont = node.getAttribute('data-round-progress-label-font') || '50pt Calibri'; return {
pre: function preLink(scope, instanceElement, instanceAttributes, controller) {
var expression = canvas.getAttribute('data-round-progress-model');
//监听模型, O了
//就监听一个属性;
scope.$watch(expression, function (newValue, oldValue) {
// Create the content of the canvas
//包括新建和重绘;
var ctx = canvas.getContext('2d');
ctx.clearRect(0, 0, width, height); // The "background" circle
var x = width / 2;
var y = height / 2;
ctx.beginPath();
ctx.arc(x, y, parseInt(outerCircleRadius), 0, Math.PI * 2, false);
ctx.lineWidth = parseInt(outerCircleWidth);
ctx.strokeStyle = outerCircleBackgroundColor;
ctx.stroke(); // The inner circle
ctx.beginPath();
ctx.arc(x, y, parseInt(innerCircleRadius), 0, Math.PI * 2, false);
ctx.lineWidth = parseInt(innerCircleWidth);
ctx.strokeStyle = innerCircleColor;
ctx.stroke(); // The inner number
ctx.font = labelFont;
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.fillStyle = labelColor;
ctx.fillText(newValue.label, x, y); // The "foreground" circle
var startAngle = - (Math.PI / 2);
var endAngle = ((Math.PI * 2 ) * newValue.percentage) - (Math.PI / 2);
var anticlockwise = false;
ctx.beginPath();
ctx.arc(x, y, parseInt(outerCircleRadius), startAngle, endAngle, anticlockwise);
ctx.lineWidth = parseInt(outerCircleWidth);
ctx.strokeStyle = outerCircleForegroundColor;
ctx.stroke();
}, true);
},
post: function postLink(scope, instanceElement, instanceAttributes, controller) {}
};
}
}; var roundProgress = {
//compile里面先对dom进行操作, 再对$socpe进行监听;
compile: compilationFunction,
replace: true
};
return roundProgress;
}]);
</script>
</body>
</html>

angular的canvas画图例子的更多相关文章

  1. SVG和canvas画图,js求数组最大最小值

    windows命令行的内容怎么复制,右键选择标记,选中内容后再点击鼠标右键就复制了. 安装Node.js后再用npm install命令会出现如下warn:saveError ENOENT: no s ...

  2. 解决canvas画图模糊的问题

    canvas 画图经常发现他是模糊的.解决这个问题主要从两个方面下手. 改变canvas渲染的像素情况:画1像素的线条看起来模糊不清,好像更宽的样子. 解决方案 var ctx = canvas.ge ...

  3. html5之canvas画图基础

    HTML5+CSS3的好处是,你可以编写一个页面分别用于不同的平台,只需要设置不同的css样式就可以了,现在基本主流浏览器都支持全新的HTML5和CSS3,因为它的跨平台开发.因为是原生代码所以它的页 ...

  4. Canvas画图在360浏览器中跑偏的问题

    问题描述,canvas画图的js代码中编写的是画正方形的代码,结果在360浏览器上变成了长方形,不知道怎么回事,请问各位大神是否遇到过此类问题? <!DOCTYPE html> <h ...

  5. h5 canvas 画图

    h5 canvas 画图 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&q ...

  6. html5 Canvas画图3:1px线条模糊问题

    点击查看原文地址: html5 Canvas画图3:1px线条模糊问题 本文属于<html5 Canvas画图系列教程> 接上一篇canvas画线条教程 上次我们讲到,canvas有时候会 ...

  7. 使用 canvas 画图时图像文字模糊的解决办法

    最近在使用 canvas 画图的时候,遇到了图像文字模糊的问题,解决思路就是根据分辨率创建不同尺寸的画布.以下是创建高分辨率画布的代码: /** * 创建高分辨率画布 * @param w 画布宽 * ...

  8. html5 canvas 画图移动端出现锯齿毛边的解决方法

    使用HTML5的canvas元素画出来的.在移动端手机上测试都发现画图有一点锯齿问题 出现这个问题的原因应该是手机的宽是720像素的, 而这个canvas是按照小于720像素画出来的, 所以在720像 ...

  9. HTML5 canvas画图

    HTML5 canvas画图 HTML5 <canvas> 标签用于绘制图像(通过脚本,通常是 JavaScript).不过,<canvas> 元素本身并没有绘制能力(它仅仅是 ...

随机推荐

  1. ejabberd常见配置说明

    1.数据库配置 ejabberd默认安装后使用的是自带的数据库,可以通过配置使用其他的数据库如Mysql.Sqlserver.Postgresql等数据库,Mysql数据库配置参见<Linux下 ...

  2. ubuntu自带的gedit编辑器添加Markdown预览插件

    gedit安装Markdown Preview Ubuntu自带的gedit编辑器也是有很强大的功能的,且支持插件的安装.对于喜欢用Markdown的我来说,这当然是很好的了,gedit本身 就支持M ...

  3. Combine small files to Sequence file

    Combine small files to sequence file or avro files are a good method to feed hadoop. Small files in ...

  4. 为TFS配置跨平台的生成服务器Xplat (Ubuntu Linux)

    1. 概述 从TFS 2015开始,微软开始支持跨平台的构建代理.你可以使用TFS的Xplat代理,方便的在基于IOS, Unix和Linux的服务器上搭建生成代理,实现构建.发布等功能.本文档已Ub ...

  5. 初始化 Ubuntu Trusty 14.04

    1. 软件源 sudo vim /etc/apt/source.list # 将软件源改为 sohu 的 deb http://mirrors.sohu.com/ubuntu/ trusty main ...

  6. Unity-WIKI 之 AnimationToPNG

    组件功能 把3D角色的动画录制成PNG一帧一帧输出,这是一个件多么美好的事! 可能遇到问题 有可能当你新建完脚本时会出现下面的错误: `System.IO.File' does not contain ...

  7. java 21-11 数据输入、输出流和内存操作流

    IO数据流: 可以读写基本数据类型的数据 数据输入流:DataInputStream DataInputStream(InputStream in)   数据输出流:DataOutputStream ...

  8. Nginx反向代理+负载均衡简单实现(https方式)

    背景:A服务器(192.168.1.8)作为nginx代理服务器B服务器(192.168.1.150)作为后端真实服务器 现在需要访问https://testwww.huanqiu.com请求时从A服 ...

  9. 各浏览器对typeof运算符的实现差异

    1,IE6/7/8中typeof运算符对BOM对象如window,document,location,history等对象的方法返回“object”,标准浏览器都返回“function”. 1 2 3 ...

  10. WMI入门

    转:http://www.cnblogs.com/ceachy/archive/2013/03/21/WMI_What.html WMI入门(一):什么是WMI WMI出现至今已经二十多年了,但很多人 ...