首页 > 其他分享 >angular的canvas画图例子

angular的canvas画图例子

时间:2023-01-13 15:33:18浏览次数:40  
标签:node canvas ctx 画图 round var progress data angular

angular的例子:

<!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>

 

天道酬勤



标签:node,canvas,ctx,画图,round,var,progress,data,angular
From: https://blog.51cto.com/u_12304260/6006080

相关文章

  • angular例子笔记
    学习angular的插件写法和制作; <!DOCTYPEhtml><htmlng-app="APP"><head><metacharset="UTF-8"><title>angular-refreshexample</title><scriptsrc="h......
  • angularJS中-$route路由-$http(ajax)的使用
    后台请求使用的是nodeJS驱动(后面帖代码),很简单的RESTFUL,页面使用的是bottstarp3.0(懒人神器); 第一个例子:在本地架设NODEJS,angular的所有请求都是请求本地的300......
  • angular_$inject
    <!DOCTYPEHTML><htmllang="zh-cn"ng-app="MainApp"><head><metacharset="UTF-8"><title>explicit-inject-service</title></head><body><divng-controller="My......
  • Angularjs——初识AngularJS
    AngularJS——初识AngularJSAngularJS是什么AngularJS是Google开源的一款前端JS结构化框架,它通过对前端开发进行分层,极好地规范了前端开发的风格——它将前端开发分为Con......
  • Angular集成bpmn.js的基础实现及扩展(一)
    一、bpmn的基本认识bpmn.js是一个BPMN2.0渲染工具包和web建模器,使得画流程图的功能在前端来完成。bpmn画图具有哪些内容?二、使用npm安装bpmn.jsnpminstall--sav......
  • 【javascript】关于 canvas.toDataURL()
    在工作中遇到了奇怪的问题,在此记录。 一、定义canvas.toDataURL()方法是返回一个包含图片展示的数据URL。可以使用 type 参数其类型,默认为PNG格式,图片的分辨率为9......
  • 前端使用canvas实现贪吃蛇小游戏
    贪吃蛇是一款益智小游戏,通过点击上下左右四个按键控制蛇头移动。设计思路主要是设计蛇的身体和移动方法。将蛇的身体设置成一个数组,数组里存放蛇身每一节的xy坐标。游......
  • 像大师一样画图-PCB常用设置与操作-PCB系列教程3-2
    PCB常用系统参数设置点击屏幕在屏幕右上角的“齿轮图标”,如图,或者执行菜单命令“工具→优先选项”(快捷键“TP”),然后选择“PCBEditor”,可以设置PCB的系统参数。PCB常规设置......
  • TypeScript 2.0 与 AngularJS 2.0 的最新动态
    Lightbot微软终于发布了TypeScript2.0的第一个RC版本。TypeScript是一个简化版的JavaScript语言,被大量用于各种Web项目,包括下面提到的著名的AngularJS框架......
  • pyechart画图(1)安装和基本操作
    安装pipinstallpyecharts==0.1.9.4 https://blog.csdn.net/weixin_43735353/article/details/89328048 Line(折线/面积图)折线图是用折线将各个数据点标志连接起......