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 相关文章推荐
基础的prototype.js常用函数及其用法
Mar 10 Javascript
30个精美的jQuery幻灯片效果插件和教程
Aug 23 Javascript
jquery如何通过name名称获取当前name的value值
Dec 20 Javascript
纯JavaScript代码实现移动设备绘图解锁
Oct 16 Javascript
jQuery日历插件datepicker用法详解
Mar 03 Javascript
AngularJS 过滤器的简单实例
Jul 27 Javascript
JS实现页面中所有img对象添加onclick事件及新窗口查看图片的方法
Dec 27 Javascript
jQuery动态生成表格及右键菜单功能示例
Jan 13 Javascript
js实现简单的获取验证码按钮效果
Mar 03 Javascript
详解express与koa中间件模式对比
Aug 07 Javascript
jQuery EasyUI Layout实现tabs标签的实例
Sep 26 jQuery
小程序云开发实战小结
Oct 25 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小型企业库存管理系统的设计与实现代码
2011/05/16 PHP
PHP is_subclass_of函数的一个BUG和解决方法
2014/06/01 PHP
解决CodeIgniter伪静态失效
2014/06/09 PHP
php截取中文字符串函数实例
2015/02/23 PHP
在html文件中也可以执行php语句的方法
2015/04/09 PHP
php的laravel框架快速集成微信登录的方法
2016/12/12 PHP
Laravel 实现数据软删除功能
2019/08/21 PHP
PHP使用PhpSpreadsheet操作Excel实例详解
2020/03/26 PHP
JS中剪贴板兼容性、判断复制成功或失败
2021/03/09 Javascript
NodeJs中的非阻塞方法介绍
2012/06/05 NodeJs
javascript基础知识大全 便于大家学习,也便于我自己查看
2012/08/17 Javascript
jquery validate 自定义验证方法介绍 日期验证
2014/02/27 Javascript
比例尺、缩略图、平移缩放之百度地图添加控件方法
2015/08/03 Javascript
javascript日期格式化方法小结
2015/12/17 Javascript
JS设置下拉列表框当前所选值的方法
2015/12/22 Javascript
浅谈JavaScript 标准对象
2016/06/02 Javascript
关于webuploader插件使用过程遇到的小问题
2016/11/07 Javascript
JS代码实现电脑配置检测功能
2018/03/21 Javascript
python网络编程学习笔记(八):XML生成与解析(DOM、ElementTree)
2014/06/09 Python
仅用500行Python代码实现一个英文解析器的教程
2015/04/02 Python
python执行CMD指令,并获取返回的方法
2018/12/19 Python
对Xpath 获取子标签下所有文本的方法详解
2019/01/02 Python
Python代码实现删除一个list里面重复元素的方法
2019/04/02 Python
python+openCV调用摄像头拍摄和处理图片的实现
2019/08/06 Python
Python上下文管理器类和上下文管理器装饰器contextmanager用法实例分析
2019/11/07 Python
python实现tail -f 功能
2020/01/17 Python
纯CSS3实现鼠标悬停提示气泡效果
2014/02/28 HTML / CSS
2014年圣诞节倒计时网页的制作过程
2014/12/05 HTML / CSS
菲律宾票务网站:StubHub菲律宾
2018/04/21 全球购物
高中生自我评语大全
2014/01/19 职场文书
学校安全教育制度
2014/01/31 职场文书
幼儿园春季开学寄语
2014/04/03 职场文书
传播学专业毕业生自荐书
2014/07/01 职场文书
以幸福为主题的活动方案
2014/08/22 职场文书
要账委托书范本
2014/09/15 职场文书
2014光棍节大学生联谊活动方案
2014/10/10 职场文书