首頁  >  文章  >  web前端  >  angularJS結合canvas畫圖範例_AngularJS

angularJS結合canvas畫圖範例_AngularJS

WBOY
WBOY原創
2016-05-16 16:15:101593瀏覽

這裡跟大家分享一個angularJS結合canvas畫圖範例,效果非常不錯,讚一個先。

複製程式碼 程式碼如下:




   
 


 
   

   

   
    <script><br />                                 地      var APP = angular.module('APP', ['angular.directives-round-progress']).<br />      controller('MainCtrl', function($scope) {<br />         $scope.roundProgressData = {<br />           //這是初始化的資料;<br />           label: 11,<br />           percentage: 0.11<br />         }<br />         //透過監聽scope下的這個roundProgressData屬性, 對介面的canvas進行重繪;<br />         $scope.$watch('roundProgressData', function (newValue) {<br />           newValue.percentage = newValue.label / 100;<br />         }, true);<br />       });<br />     </script>
<script><br />     /*!<br />  * AngularJS Round Progress Directive<br />  *<br />  * Copyright 2013 Stephane Begaudeau<br />  * Released under the MIT license<br />  */<br /> angular.module('angular.directives-round-progress', []).directive('angRoundProgress', [function () {<br />   var compilationFunction = function (templateElement, templateAttributes, transclude) {<br />     if (templateElement.length === 1) {<br />       //初始化DOM模型, 包括初始化canvas等;<br />       var node = templateElement[0];<br />       var width = node.getAttribute('data-round-progress-width') || '400';<br />       var height = node.getAttribute('data-round-progress-height') || '400';<br />       var canvas = document.createElement('canvas');<br />       canvas.setAttribute('width', width);<br />       canvas.setAttribute('height', height);<br />       canvas.setAttribute('data-round-progress-model', node.getAttribute('data-round-progress-model'));<br />         //相當於demo, 取代原來的元素;<br />       node.parentNode.replaceChild(canvas, node);<br />         //各種設定;<br />       var outerCircleWidth = node.getAttribute('data-round-progress-outer-circle-width') || '20';<br />       var innerCircleWidth = node.getAttribute('data-round-progress-inner-circle-width') || '5';<br />       var outerCircleBackgroundColor = node.getAttribute('data-round-progress-outer-circle-background-color') || '#505769';<br />       var outerCircleForegroundColor = node.getAttribute('data-round-progress-outer-circle-foreground-color') || '#12eeb9';<br />       var innerCircleColor = node.getAttribute('data-round-progress-inner-circle-color') || '#505769';<br />       var labelColor = node.getAttribute('data-round-progress-label-color') || '#12eeb9';<br />       var outerCircleRadius = node.getAttribute('data-round-progress-outer-circle-radius') || '100';<br />       var innerCircleRadius = node.getAttribute('data-round-progress-inner-circle-radius') || '70';<br />       var labelFont = node.getAttribute('data-round-progress-label-font') || '50pt Calibri';<br />       return {<br />         pre: function preLink(scope, instanceElement, instanceAttributes, controller) {<br />           var expression = canvas.getAttribute('data-round-progress-model');<br />             //監聽模型, O了<br />             //就監聽一個屬性;<br />           scope.$watch(expression, function (newValue, oldValue) {<br />             // Create the content of the canvas<br />            //包含新建與重繪;<br />             var ctx = canvas.getContext('2d');<br />             ctx.clearRect(0, 0, 寬度, 高度);<br />             // 「背景」圓<br />             var x = 寬度 / 2;<br />             var y = 高度 / 2;<br />             ctx.beginPath();<br />             ctx.arc(x, y, parseInt(outerCircleRadius), 0, Math.PI * 2, false);<br />             ctx.lineWidth = parseInt(outerCircleWidth);<br />             ctx.筆畫樣式 = 外圓背景色;<br />             ctx.中風();<br />             // 內圓<br />             ctx.beginPath();<br />             ctx.arc(x, y, parseInt(innerCircleRadius), 0, Math.PI * 2, false);<br />             ctx.lineWidth = parseInt(innerCircleWidth);<br />             ctx.StrongStyle = innerCircleColor;<br />             ctx.中風();<br />             // 內部數字<br />             ctx.font = labelFont;<br />             ctx.textAlign = 'center';<br />             ctx.textBaseline = '中';<br />             ctx.fillStyle = labelColor;<br />             ctx.fillText(newValue.label, x, y);<br />             // 「前景」圓<br />             var startAngle = - (Math.PI / 2);<br />             var endAngle = ((Math.PI * 2 ) * newValue.percentage) - (Math.PI / 2);<br />             var 逆時針 = false;<br />             ctx.beginPath();<br />             ctx.arc(x, y, parseInt(outerCircleRadius), startAngle, endAngle, 逆時針);<br />             ctx.lineWidth = parseInt(outerCircleWidth);<br />             ctx.筆畫樣式=outerCircleForegroundColor;<br />             ctx.中風();<br />           },正確);<br />         },<br />         post: 函數 postLink(範圍、instanceElement、instanceAttributes、控制器) {}<br />       };<br />     }<br />   };<br />   var roundProgress = {<br />       //編譯內先對dom操作,再對$socpe進行監聽;<br />     編譯:編譯函數,<br />     替換:true<br />   };<br />   返回回合進度;<br /> }]);<br /> </腳本><br /> </身體><br /> </script>
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn