angularJS結合canvas實現的畫圖程式碼例項
本章節介紹分享一段程式碼例項,它實現angularJS結合canvas實現作圖效果。
程式碼例項:
[HTML] 純文字檢視 複製程式碼<!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta name="author" content="http://www.softwhy.com/" /> <title>螞蟻部落</title> <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>
相關文章
- canvas刮刮樂程式碼例項Canvas
- canvas載入效果程式碼例項Canvas
- canvas繪製扇形程式碼例項Canvas
- canvas原型鐘錶效果程式碼例項Canvas原型
- canvas繪製網格程式碼例項Canvas
- canvas氣泡上浮效果程式碼例項Canvas
- PE結構-合併節(附例項程式碼)
- canvas繪製機器貓程式碼例項Canvas
- canvas繪製拋物線程式碼例項Canvas線程
- canvas translate()、scale()和rotate()方法程式碼例項Canvas
- canvas繪製箭頭效果程式碼例項Canvas
- canvas繪製圓形鐘錶程式碼例項Canvas
- Python實現簡單網頁圖片抓取完整程式碼例項Python網頁
- html實現簡單ListViews效果的例項程式碼HTMLView
- web實戰:video結合canvas實現視訊線上截圖WebIDECanvas
- html5 canvas 實現光線沿不規則路徑運動例項程式碼HTMLCanvas
- 結合例項學習|字元編碼和解碼字元
- JavaScript陣列合並程式碼例項JavaScript陣列
- vuejs實現新增tag標籤程式碼例項VueJS
- 例項程式碼分享Python實現Linux監控PythonLinux
- 抽象結合例項 Employee抽象
- MyCat分片:水平拆分例項解析和程式碼實現!
- 正則實現個位數補零程式碼例項
- canvas之實現控制動畫Canvas動畫
- python 單一程式例項 實現Python
- SwiftUI 官方畫圖例項詳細解析SwiftUI
- canvas 例項之鬧鐘Canvas
- Python物件導向多型實現原理及程式碼例項Python物件多型
- canvas實現二維碼和圖片合成Canvas
- CSS3圖層陰影程式碼例項CSSS3
- 資料結構 - 圖之程式碼實現資料結構
- JDK 7 新特性小結(包含例項程式碼)JDK
- 使用canvas實現簡單動畫Canvas動畫
- canvas拼圖功能實現Canvas
- canvas 實現雷達圖Canvas
- canvas實現截圖功能Canvas
- canvas畫圖採坑Canvas
- dom操作程式碼例項
- css梯形程式碼例項CSS