angularJS结合canvas画图例子


Posted in Javascript onFebruary 09, 2015

这里给大家分享一个angularJS结合canvas画图例子,效果非常不错,赞一个先。

<!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>

以上就是angularJS结合canvas画图例子的全部代码了,希望大家能够喜欢。

Javascript 相关文章推荐
js 分栏效果实现代码
Aug 29 Javascript
IE JS无提示关闭窗口不提示的方法
Apr 29 Javascript
使用javascript做的一个随机点名程序
Feb 13 Javascript
JQuery EasyUI 数字格式化处理示例
May 05 Javascript
js实现弹出窗口、页面变成灰色并不可操作的例子分享
May 10 Javascript
基于js里调用函数时,函数名带括号和不带括号的区别
Jul 28 Javascript
浅谈bootstrap使用中的一些问题以及解决过程
Oct 18 Javascript
jquery的父、子、兄弟节点查找,节点的子节点循环方法
Dec 07 Javascript
vue双向数据绑定原理探究(附demo)
Jan 17 Javascript
bootstrap实现动态进度条效果
Mar 08 Javascript
Bootstrap响应式表格详解
May 23 Javascript
vue项目创建步骤及路由router
Jan 14 Javascript
jquery实现上下左右滑动的方法
Feb 09 #Javascript
js实现上传图片预览的方法
Feb 09 #Javascript
js实现ifram取父窗口URL地址的方法
Feb 09 #Javascript
jquery实现相册一下滑动两次的方法
Feb 09 #Javascript
js点击选择文本的方法
Feb 09 #Javascript
JS动态加载当前时间的方法
Feb 09 #Javascript
JavaScript实现Java中StringBuffer的方法
Feb 09 #Javascript
You might like
PHP IDE PHPStorm配置支持友好Laravel代码提示方法
2015/05/12 PHP
php is_writable判断文件是否可写实例代码
2016/10/13 PHP
PHP中的浅复制与深复制的实例详解
2017/10/26 PHP
PHP生成加减算法方式的验证码实例
2018/03/12 PHP
网页右下角弹出窗体实现代码
2014/06/05 Javascript
不到30行JS代码实现Excel表格的方法
2014/11/15 Javascript
基于KO+BootStrap+MVC实现的分页控件代码分享
2016/11/07 Javascript
JS非空验证及邮箱验证的实例
2017/08/11 Javascript
JQuery实现table中tr上移下移的示例(超简单)
2018/01/08 jQuery
基于casperjs和resemble.js实现一个像素对比服务详解
2018/01/10 Javascript
react router4+redux实现路由权限控制的方法
2018/05/03 Javascript
关于vue的语法规则检测报错问题的解决
2018/05/21 Javascript
JavaScript实现的3D旋转魔方动画效果实例代码
2019/07/31 Javascript
微信小程序实现弹框效果
2020/05/26 Javascript
[02:06]2018完美世界全国高校联赛秋季赛开始报名(附彩蛋)
2018/09/03 DOTA
[01:04:02]DOTA2-DPC中国联赛 正赛 Elephant vs IG BO3 第二场 1月24日
2021/03/11 DOTA
python采用getopt解析命令行输入参数实例
2014/09/30 Python
Python3导入自定义模块的三种方法详解
2018/04/13 Python
在pandas多重索引multiIndex中选定指定索引的行方法
2018/11/16 Python
Python在Matplotlib图中显示中文字体的操作方法
2019/07/29 Python
python RC4加密操作示例【测试可用】
2019/09/26 Python
Python实现桌面翻译工具【新手必学】
2020/02/12 Python
python函数enumerate,operator和Counter使用技巧实例小结
2020/02/22 Python
Pytorch mask-rcnn 实现细节分享
2020/06/24 Python
python脚本第一行如何写
2020/08/30 Python
X/HTML5 和 XHTML2
2008/10/17 HTML / CSS
全球摩托车装备领导者:RevZilla
2017/09/04 全球购物
美国巧克力喷泉品牌:Sephra
2019/05/05 全球购物
银行员工辞职信范文
2014/01/20 职场文书
优秀管理者获奖感言
2014/02/17 职场文书
学习十八大报告感言
2014/02/28 职场文书
总结表彰大会主持词
2014/03/26 职场文书
2015员工年度考核评语
2015/03/25 职场文书
一文搞懂如何实现Go 超时控制
2021/03/30 Python
php png失真的原因及解决办法
2021/11/17 PHP
Spring Boot 的创建和运行示例代码详解
2022/07/23 Java/Android