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 相关文章推荐
jQuery 表单验证扩展代码(一)
Oct 11 Javascript
js数组的操作详解
Mar 27 Javascript
从数据结构的角度分析 for each in 比 for in 快的多
Jul 07 Javascript
Bootstrap列表组学习使用
Feb 09 Javascript
ReactJs设置css样式的方法
Jun 08 Javascript
node中Express 动态设置端口的方法
Aug 04 Javascript
浅谈Vue-cli单文件组件引入less,sass,css样式的不同方法
Mar 13 Javascript
js中位运算的运用实例分析
Dec 11 Javascript
VUE脚手架的下载和配置步骤详解
Apr 01 Javascript
微信小程序实现bindtap等事件传参
Apr 08 Javascript
vue组件三大核心概念图文详解
May 30 Javascript
利用vue-i18n实现多语言切换效果的方法
Jun 19 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变量存储的详解
2013/06/13 PHP
php实现在线生成条形码示例分享(条形码生成器)
2013/12/30 PHP
php使用ereg验证文件上传的方法
2014/12/16 PHP
自己写的php中文截取函数mb_strlen和mb_substr
2015/02/09 PHP
PHP基于单例模式实现的mysql类
2016/01/09 PHP
PHP编程求最大公约数与最小公倍数的方法示例
2017/05/29 PHP
ThinkPHP删除栏目(实现批量删除栏目)
2017/06/21 PHP
解决php用mysql方式连接数据库出现Deprecated报错问题
2019/12/25 PHP
IE6下通过a标签点击切换图片的问题
2010/11/14 Javascript
Jquery解析json数据详解
2013/12/26 Javascript
JS图像无缝滚动脚本非常好用
2014/02/10 Javascript
javascript制作的cookie封装及使用指南
2015/01/02 Javascript
jquery实现页面百叶窗走马灯式翻滚显示效果的方法
2015/03/12 Javascript
jQuery AjaxUpload 上传图片代码
2016/02/02 Javascript
14 个折磨人的 JavaScript 面试题
2016/08/08 Javascript
AngularJs html compiler详解及示例代码
2016/09/01 Javascript
Vue报错:Uncaught TypeError: Cannot assign to read only property’exports‘ of object’#‘的解决方法
2017/06/17 Javascript
websocket4.0+typescript 实现热更新的方法
2019/08/14 Javascript
[02:19]DOTA选手解说齐贺岁
2018/02/11 DOTA
[02:51]DOTA2 Supermajor小组分组对阵抽签仪式
2018/06/01 DOTA
Python使用urllib2获取网络资源实例讲解
2013/12/02 Python
Python3连接SQLServer、Oracle、MySql的方法
2018/06/28 Python
Python基于Opencv来快速实现人脸识别过程详解(完整版)
2019/07/11 Python
python中append实例用法总结
2019/07/30 Python
Python做图像处理及视频音频文件分离和合成功能
2020/11/24 Python
Python接口自动化系列之unittest结合ddt的使用教程详解
2021/02/23 Python
CSS3 函数技巧 用css 实现js实现的事情(clac Counters Tooltip)
2017/08/15 HTML / CSS
Html5自定义字体解决方法
2019/10/09 HTML / CSS
葛优非诚勿扰搞笑征婚台词
2014/03/17 职场文书
校园环境卫生倡议书
2015/04/29 职场文书
未婚证明范本
2015/06/15 职场文书
Ajax 的初步实现(使用vscode+node.js+express框架)
2021/06/18 Javascript
MySQL非空约束(not null)案例讲解
2021/08/23 MySQL
【DOTA2】当街暴打?PSG LGD vs VG - DPC 2022 WINTER TOUR CN
2022/04/02 DOTA
Python3使用Qt5来实现简易的五子棋小游戏
2022/05/02 Python
MySQL的意向共享锁、意向排它锁和死锁
2022/07/15 MySQL