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 01 Javascript
javascript获取元素CSS样式代码示例
Nov 28 Javascript
js写的评论分页(还不错)
Dec 23 Javascript
Jquery下EasyUI组件中的DataGrid结果集清空方法
Jan 06 Javascript
jQuery中ajax的load()与post()方法实例详解
Jan 05 Javascript
jQuery使用ajax跨域获取数据的简单实例
May 18 Javascript
bootstrap表格分页实例讲解
Dec 30 Javascript
jQuery插件zTree实现获取当前选中节点在同级节点中序号的方法
Mar 08 Javascript
详解创建自定义的Angular Schematics
Jun 06 Javascript
vue.js中ref和$refs的使用及示例讲解
Aug 14 Javascript
javascript 易错知识点实例小结
Apr 25 Javascript
JavaScript数组排序功能简单实现
May 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使用正则验证中文
2016/04/06 PHP
jquery获取被勾选的checked(选中)的那一行的3列和4列的值
2013/07/04 Javascript
用javascript关闭本窗口不弹出询问框的方法
2014/09/12 Javascript
jquery获取select选中值的方法分析
2015/12/22 Javascript
很棒的Bootstrap选项卡切换效果
2016/07/01 Javascript
javascript insertAfter()定义与用法示例
2016/07/25 Javascript
jQuery实用小技巧_输入框文字获取和失去焦点的简单实例
2016/08/25 Javascript
javascript的函数劫持浅析
2016/09/26 Javascript
快速入门Vue
2016/12/19 Javascript
AngularJS实现网站换肤实例
2021/02/19 Javascript
关于ES6的六个小特性(二)
2017/02/20 Javascript
Angular2库初探
2017/03/01 Javascript
AngularJS2中一种button切换效果的实现方法(二)
2017/03/27 Javascript
JS中移除非数字最多保留一位小数
2018/05/09 Javascript
Vue表单控件绑定图文详解
2019/02/11 Javascript
vue组件中iview的modal组件爬坑问题之modal的显示与否应该是使用v-show
2019/04/12 Javascript
JavaScript中的执行环境和作用域链
2020/09/04 Javascript
vue.js封装switch开关组件的操作
2020/10/26 Javascript
[01:23]一分钟告诉你 DOTA2为什么叫信仰2
2014/06/20 DOTA
[01:32]DOTA2上海特锦赛现场采访:最想COS的英雄
2016/03/25 DOTA
python实现在windows服务中新建进程的方法
2015/06/30 Python
Python利用ElementTree模块处理XML的方法详解
2017/08/31 Python
Tensorflow的可视化工具Tensorboard的初步使用详解
2018/02/11 Python
Python利用sqlacodegen自动生成ORM实体类示例
2019/06/04 Python
基于OpenCV的路面质量检测的实现
2020/11/04 Python
Django vue前后端分离整合过程解析
2020/11/20 Python
伦敦一家西班牙童装精品店:La Coqueta
2018/02/02 全球购物
Bally澳大利亚官网:瑞士奢侈品牌
2018/11/01 全球购物
学校元旦晚会方案
2014/02/19 职场文书
初三班主任寄语大全
2014/04/04 职场文书
爱情寄语大全
2014/04/09 职场文书
综合内勤岗位职责
2014/04/14 职场文书
大学生通用个人自我评价
2014/04/27 职场文书
家具商场的活动方案
2014/08/16 职场文书
js实现上传图片到服务器
2021/04/11 Javascript
Css预编语言及区别详解
2021/04/25 HTML / CSS