angularJS結合canvas畫圖範例_AngularJS
May 16, 2016 pm 04:15 PM
angularjs
canvas
這裡跟大家分享一個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>
<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

熱門文章
R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 週前
By 尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
3 週前
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前
By 尊渡假赌尊渡假赌尊渡假赌
擊敗分裂小說需要多長時間?
3 週前
By DDD
公眾號網頁更新緩存難題:如何避免版本更新後舊緩存影響用戶體驗?
3 週前
By 王林

熱門文章
R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 週前
By 尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
3 週前
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前
By 尊渡假赌尊渡假赌尊渡假赌
擊敗分裂小說需要多長時間?
3 週前
By DDD
公眾號網頁更新緩存難題:如何避免版本更新後舊緩存影響用戶體驗?
3 週前
By 王林

熱門文章標籤

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)