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.Autocomplete插件 支持中文输入法 避免TAB、ENTER键失效、导致表单提交
Oct 11 Javascript
JavaScript类库D
Oct 24 Javascript
在浏览器中实现图片粘贴的jQuery插件-- pasteimg使用指南
Dec 29 Javascript
javascript批量修改文件编码格式的方法
Jan 27 Javascript
兼容各大浏览器的JavaScript阻止事件冒泡代码
Jul 09 Javascript
以jQuery中$.Deferred对象为例讲解promise对象是如何处理异步问题
Nov 13 Javascript
实例代码讲解jquery easyui动态tab页
Nov 17 Javascript
jQuery插件HighCharts实现的2D回归直线散点效果示例【附demo源码下载】
Mar 09 Javascript
Jquery EasyUI $.Parser
Jun 02 jQuery
javascript用rem来做响应式开发
Jan 13 Javascript
JavaScript实现表单验证功能
Dec 09 Javascript
Vue+TypeScript中处理computed方式
Apr 02 Vue.js
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 文章采集正则代码
2009/12/28 PHP
一个图片地址分解程序(用于PHP小偷程序)
2014/08/23 PHP
PHP7 标准库修改
2021/03/09 PHP
JavaScript 继承详解(三)
2009/07/13 Javascript
jquery获取checkbox的值并post提交
2015/01/14 Javascript
JavaScript使用cookie实现记住账号密码功能
2015/04/27 Javascript
JavaScript学习笔记之数组随机排序
2016/03/23 Javascript
详细探究ES6之Proxy代理
2016/07/22 Javascript
Jquery实时监听input value的实例
2017/01/26 Javascript
基于ExtJs在页面上window再调用Window的事件处理方法
2017/07/26 Javascript
vue.js实现插入数值与表达式的方法分析
2018/07/06 Javascript
webstrom Debug 调试vue项目的方法步骤
2018/07/17 Javascript
bootstrap-closable-tab可实现关闭的tab标签页插件
2020/08/09 Javascript
JavaScript日期库date-fn.js使用方法解析
2020/09/09 Javascript
[01:14:05]《加油DOTA》第四期
2014/08/25 DOTA
python实现的系统实用log类实例
2015/06/30 Python
在python中使用正则表达式查找可嵌套字符串组
2017/10/24 Python
Python爬虫实战:分析《战狼2》豆瓣影评
2018/03/26 Python
Python3中关于cookie的创建与保存
2018/10/21 Python
Django组件cookie与session的具体使用
2019/06/05 Python
pymysql模块的操作实例
2019/12/17 Python
Anaconda的安装及其环境变量的配置详解
2020/04/22 Python
python3 kubernetes api的使用示例
2021/01/12 Python
Python 将代码转换为可执行文件脱离python环境运行(步骤详解)
2021/01/25 Python
澳大利亚玩具剧场:Toy Playhouse
2019/03/03 全球购物
你们项目是如何进行变更控制的
2015/08/26 面试题
医学院四年学习生活的自我评价
2013/11/06 职场文书
中专生职业生涯规划书范文
2013/12/29 职场文书
国培计划培训感言
2014/03/11 职场文书
机关领导查摆四风思想汇报
2014/09/13 职场文书
户外活动总结
2015/02/04 职场文书
2015年教师节活动总结
2015/03/20 职场文书
公司总经理岗位职责
2015/04/01 职场文书
钢琴师观后感
2015/06/12 职场文书
迎新生晚会主持词
2015/06/30 职场文书
Python使用pyecharts控件绘制图表
2022/06/05 Python