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 相关文章推荐
图片之间的切换
Jun 26 Javascript
jquery的颜色选择插件实例代码
Oct 02 Javascript
JS注册/移除事件处理程序(ExtJS应用程序设计实战)
May 07 Javascript
jquery滚动组件(vticker.js)实现页面动态数据的滚动效果
Jul 03 Javascript
Validform验证时可以为空否则按照指定格式验证
Oct 20 Javascript
js闭包学习心得总结
Apr 17 Javascript
详解基于React.js和Node.js的SSR实现方案
Mar 21 Javascript
JSX在render函数中的应用详解
Sep 04 Javascript
重置Redux的状态数据的方法实现
Nov 18 Javascript
js模拟实现烟花特效
Mar 10 Javascript
微信小程序swiper组件实现抖音翻页切换视频功能的实例代码
Jun 24 Javascript
Vue过滤器,生命周期函数和vue-resource简单介绍
Jan 12 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
C#使用PHP服务端的Web Service通信实例
2014/04/08 PHP
CodeIgniter安全相关设置汇总
2014/07/03 PHP
PHP获取mysql数据表的字段名称和详细信息的方法
2014/09/27 PHP
使用php转义输出HTML到JavaScript
2015/03/27 PHP
PHP的Yii框架中移除组件所绑定的行为的方法
2016/03/18 PHP
PHP文字转图片功能原理与实现方法分析
2017/08/31 PHP
PHP常见过waf webshell以及最简单的检测方法
2019/05/21 PHP
php求斐波那契数的两种实现方式【递归与递推】
2019/09/09 PHP
javascript中动态加载js文件多种解决办法总结
2013/11/15 Javascript
transport.js和jquery冲突问题的解决方法
2015/02/10 Javascript
JavaScript将字符串转换成字符编码列表的方法
2015/03/19 Javascript
JavaScript时间操作之年月日星期级联操作
2016/01/15 Javascript
JS JSOP跨域请求实例详解
2016/07/04 Javascript
js 将图片连接转换成base64格式的简单实例
2016/08/10 Javascript
jQuery简单创建节点的方法
2016/09/09 Javascript
Bootstrap CSS组件之按钮组(btn-group)
2016/12/17 Javascript
JavaScript中的call和apply的用途以及区别
2017/01/11 Javascript
微信小程使用swiper组件实现图片轮播切换显示功能【附源码下载】
2017/12/12 Javascript
小程序实现发表评论功能
2018/07/06 Javascript
超简单的微信小程序轮播图
2019/11/22 Javascript
小程序中手机号识别的示例
2020/12/14 Javascript
python妹子图简单爬虫实例
2015/07/07 Python
python实现rsa加密实例详解
2017/07/19 Python
在pandas中一次性删除dataframe的多个列方法
2018/04/10 Python
Selenium 模拟浏览器动态加载页面的实现方法
2018/05/16 Python
python 实现倒排索引的方法
2018/12/25 Python
iHerb台湾:维生素、保健品和健康产品
2018/01/31 全球购物
英国床和浴室商场:Bed & Bath Emporium
2018/05/20 全球购物
Bose美国官网:购买Bose耳机和音箱
2019/03/10 全球购物
abstract是什么意思
2012/02/12 面试题
外贸业务员岗位职责
2013/11/24 职场文书
军训生自我鉴定范文
2013/12/27 职场文书
群众路线教育实践活动整改方案(个人版)
2014/10/25 职场文书
解决numpy和torch数据类型转化的问题
2021/05/23 Python
关于python3 opencv 图像二值化的问题(cv2.adaptiveThreshold函数)
2022/04/04 Python
基于redis+lua进行限流的方法
2022/07/23 Redis