view-draw.uvue 8.2 KB
Newer Older
张磊 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
<template>
	<!-- #ifdef APP -->
	<scroll-view style="flex: 1">
		<!-- #endif -->
		<view>
			<view ref="draw-text-view" style="width: 750rpx;height: 550rpx; background-color: lightgray;margin: 30rpx 0rpx;"></view>
			<view ref="draw-line-view" style="width: 750rpx;height: 550rpx; background-color: lightgray;margin: 30rpx 0rpx;"></view>
			<view ref="draw-circle-view" style="width: 750rpx;height: 550rpx; background-color: lightgray;margin: 30rpx 0rpx;"></view>
			<view ref="draw-dash-line" style="width: 750rpx;height: 550rpx; background-color: lightgray;margin: 30rpx 0rpx;"></view>
			<view ref="draw-house" style="width: 750rpx;height: 550rpx; background-color: lightgray;margin: 30rpx 0rpx;"></view>
			<view ref="draw-style" style="width: 750rpx;height: 550rpx; background-color: lightgray;margin: 30rpx 0rpx;"></view>
			<view ref="draw-odd" style="width: 750rpx;height: 550rpx; background-color: lightgray;margin: 30rpx 0rpx;"></view>
		</view>
		<!-- #ifdef APP -->
	</scroll-view>
	<!-- #endif -->
</template>

<script>
	var y = 160
	export default {
		data() {
			return {
				texts: [
					'HBuilderX,轻巧、极速,极客编辑器',
					'uni-app x,终极跨平台方案',
					'uniCloud,js serverless云服务',
					'uts,大一统语言',
					'uniMPSdk,让你的App具备小程序能力',
					'uni-admin,开源、现成的全端管理后台',
					'uni-id,开源、全端的账户中心',
					'uni-pay,开源、云端一体、全平台的支付',
					'uni-ai,聚合ai能力',
					'uni-cms,开源、云端一体、全平台的内容管理平台',
					'uni-im,开源、云端一体、全平台的im即时消息',
					'uni统计,开源、完善、全平台的统计报表',
					'......'
				] as string[]
			}
		},
		onShow() {
		},

		onReady() {
			this.drawText()
			this.drawLines()
			this.drawCircles()
			this.drawStar()
			this.drawhouse()
			this.drawPoint()
			this.drawRect()
		},
		onUnload() {
			y = 160
		},
		methods: {
			drawText() {
DCloud-yyl's avatar
DCloud-yyl 已提交
58
				let node = (this.$refs['draw-text-view'] as Element)
张磊 已提交
59 60 61 62
				let ctx = node.getDrawableContext()
				let width = node.getBoundingClientRect().width
				ctx!!.reset()
				ctx!!.font = "15px"
张磊 已提交
63
				ctx!.textAlign = "center"
张磊 已提交
64 65 66 67 68 69 70 71 72 73 74
				for (var i = 0; i < this.texts.length; i++) {
					let value = this.texts[i]
					if (i % 2 == 0) {
						ctx!!.fillText(value, width / 2, (20 * (i + 1)))
					} else {
						ctx!!.strokeText(value, width / 2, (20 * (i + 1)))
					}
				}
				ctx!!.update()
			},
			drawLines() {
DCloud-yyl's avatar
DCloud-yyl 已提交
75
				let ctx = (this.$refs['draw-line-view'] as Element).getDrawableContext()
张磊 已提交
76 77 78 79
				ctx!!.reset()
				ctx!!.lineWidth = 10;

				["round", "bevel", "miter"].forEach((join, i) => {
张磊 已提交
80 81 82 83 84 85 86 87
					ctx!.lineJoin = join;
					ctx!.beginPath();
					ctx!.moveTo(5, 10 + i * 40);
					ctx!.lineTo(50, 50 + i * 40);
					ctx!.lineTo(90, 10 + i * 40);
					ctx!.lineTo(130, 50 + i * 40);
					ctx!.lineTo(170, 10 + i * 40);
					ctx!.stroke();
张磊 已提交
88 89 90
				});
				ctx!!.lineWidth = 1
				var space = 170
张磊 已提交
91 92 93 94 95 96 97
				ctx!.strokeStyle = '#09f';
				ctx!.beginPath();
				ctx!.moveTo(10 + space, 10);
				ctx!.lineTo(140 + space, 10);
				ctx!.moveTo(10 + space, 140);
				ctx!.lineTo(140 + space, 140);
				ctx!.stroke();
张磊 已提交
98
				// Draw lines
张磊 已提交
99
				ctx!.strokeStyle = 'black';
张磊 已提交
100
				['butt', 'round', 'square'].forEach((lineCap, i) => {
张磊 已提交
101 102 103 104 105 106
					ctx!.lineWidth = 15;
					ctx!.lineCap = lineCap;
					ctx!.beginPath();
					ctx!.moveTo(25 + space + i * 50, 10);
					ctx!.lineTo(25 + space + i * 50, 140);
					ctx!.stroke();
张磊 已提交
107
				});
张磊 已提交
108 109 110 111 112 113 114 115 116 117 118
				ctx!.lineWidth = 1;
				this.drawDashedLine([], ctx);
				this.drawDashedLine([2, 2], ctx);
				this.drawDashedLine([10, 10], ctx);
				this.drawDashedLine([20, 5], ctx);
				this.drawDashedLine([15, 3, 3, 3], ctx);
				this.drawDashedLine([20, 3, 3, 3, 3, 3, 3, 3], ctx);
				ctx!.lineDashOffset = 18;
				this.drawDashedLine([12, 3, 3], ctx);
				ctx!.lineDashOffset = 0
				ctx!.setLineDash([0])
张磊 已提交
119 120 121 122 123 124 125 126 127 128 129
				ctx!!.update()
			},
			drawDashedLine(pattern : Array<number>, ctx : DrawableContext) {
				ctx.beginPath();
				ctx.setLineDash(pattern);
				ctx.moveTo(0, y);
				ctx.lineTo(300, y);
				ctx.stroke();
				y += 15;
			},
			drawCircles() {
DCloud-yyl's avatar
DCloud-yyl 已提交
130
				let ctx = (this.$refs['draw-circle-view'] as Element).getDrawableContext()
张磊 已提交
131 132 133 134 135 136 137 138 139 140 141 142
				ctx!!.reset()
				// Draw shapes
				for (var i = 0; i < 4; i++) {
					for (var j = 0; j < 3; j++) {
						ctx!!.beginPath();
						var x = 25 + j * 50;               // x coordinate
						var y = 25 + i * 50;               // y coordinate
						var radius = 20;                    // Arc radius
						var startAngle = 0;                     // Starting point on circle
						var endAngle = Math.PI + (Math.PI * j) / 2; // End point on circle
						var clockwise = i % 2 == 0 ? false : true; // clockwise or anticlockwise

张磊 已提交
143
						ctx!.arc(x, y, radius, startAngle, endAngle, clockwise);
张磊 已提交
144 145

						if (i > 1) {
张磊 已提交
146
							ctx!.fill();
张磊 已提交
147
						} else {
张磊 已提交
148
							ctx!.stroke();
张磊 已提交
149 150 151 152 153 154 155
						}
					}
				}
				ctx!!.update()
			},

			drawStar() {
DCloud-yyl's avatar
DCloud-yyl 已提交
156
				let ctx = (this.$refs['draw-dash-line'] as Element).getDrawableContext()
张磊 已提交
157 158 159 160 161 162 163 164 165 166 167 168 169
				ctx!!.reset()
				ctx!!.beginPath();
				var horn = 5; // 画5个角
				var angle = 360 / horn; // 五个角的度数
				// 两个圆的半径
				var R = 50;
				var r = 20;
				// 坐标
				var x = 100;
				var y = 100;
				for (var i = 0; i < horn; i++) {
					// 角度转弧度:角度/180*Math.PI
					// 外圆顶点坐标
张磊 已提交
170
					ctx!.lineTo(Math.cos((18 + i * angle) / 180.0 * Math.PI) * R + x, -Math.sin((18 + i * angle) / 180.0 * Math.PI) * R + y);
张磊 已提交
171
					// 內圆顶点坐标
张磊 已提交
172
					ctx!.lineTo(Math.cos((54 + i * angle) / 180.0 * Math.PI) * r + x, -Math.sin((54 + i * angle) / 180.0 * Math.PI) * r + y);
张磊 已提交
173 174
				}
				// closePath:关闭路径,将路径的终点与起点相连
张磊 已提交
175 176 177 178 179 180
				ctx!.closePath();
				ctx!.lineWidth = 3;
				ctx!.fillStyle = '#E4EF00';
				ctx!.strokeStyle = "red";
				ctx!.fill();
				ctx!.stroke();
张磊 已提交
181

张磊 已提交
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
				ctx!.lineWidth = 10;
				ctx!.beginPath()
				ctx!.moveTo(170, 100)
				ctx!.lineTo(255, 15)
				ctx!.lineTo(340, 100)
				ctx!.closePath()
				ctx!.fill()
				ctx!.strokeStyle = "blue"
				ctx!.stroke()
				ctx!.beginPath()
				ctx!.moveTo(170, 145)
				ctx!.lineTo(255, 45)
				ctx!.lineTo(340, 145)
				ctx!.closePath()
				ctx!.fill()
				ctx!.strokeStyle = "gray"
				ctx!.stroke()
张磊 已提交
199
				// 未设置beginPath,导致上下表现一致,与前端一致
张磊 已提交
200 201 202 203 204 205 206 207
				ctx!.moveTo(170, 190)
				ctx!.lineTo(255, 90)
				ctx!.lineTo(340, 190)
				ctx!.closePath()
				ctx!.fillStyle = "orange"
				ctx!.fill()
				ctx!.strokeStyle = "khaki"
				ctx!.stroke()
张磊 已提交
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
				ctx!!.update()
			},
			hex(num : number) : string {
				if (num == 0) {
					return "00"
				}
				let hexChars = "0123456789ABCDEF";
				let result = "";
				while (num > 0) {
					let remainder : Int = num.toInt() % 16;
					result = hexChars[remainder] + result;
					num = Math.floor(num.toInt() / 16);
				}
				if (result.length == 1) {
					return "0" + result
				}
				return result
			},
			drawhouse() {
DCloud-yyl's avatar
DCloud-yyl 已提交
227
				let ctx = (this.$refs['draw-house'] as Element).getDrawableContext()
张磊 已提交
228 229 230 231
				ctx!!.reset()
				ctx!!.lineWidth = 10;

				// Wall
张磊 已提交
232
				ctx!.strokeRect(75, 140, 150, 110);
张磊 已提交
233 234

				// Door
张磊 已提交
235
				ctx!.fillRect(130, 190, 40, 60);
张磊 已提交
236 237

				// Roof
张磊 已提交
238 239 240 241 242 243
				ctx!.beginPath();
				ctx!.moveTo(50, 140);
				ctx!.lineTo(150, 60);
				ctx!.lineTo(250, 140);
				ctx!.closePath();
				ctx!.stroke();
张磊 已提交
244 245 246
				ctx!!.update()
			},
			drawPoint() {
DCloud-yyl's avatar
DCloud-yyl 已提交
247
				let ctx = (this.$refs['draw-style'] as Element).getDrawableContext()
张磊 已提交
248 249 250 251
				ctx!!.reset()
				for (let i = 0; i < 6; i++) {
					for (let j = 0; j < 6; j++) {
						ctx!!.strokeStyle = `rgb(0,${Math.floor(255 - 42.5 * i)},${Math.floor(255 - 42.5 * j)})`;
张磊 已提交
252 253 254
						ctx!.beginPath();
						ctx!.arc(12.5 + j * 25, 12.5 + i * 25, 10, 0, Math.PI * 2, true);
						ctx!.stroke();
张磊 已提交
255 256 257 258 259
					}
				}
				for (let i = 0; i < 6; i++) {
					for (let j = 0; j < 6; j++) {
						ctx!!.fillStyle = `rgb(${Math.floor(255 - 42.5 * i)},${Math.floor(255 - 42.5 * j)},0)`;
张磊 已提交
260
						ctx!.fillRect(180 + j * 25, i * 25, 25, 25);
张磊 已提交
261 262 263 264 265
					}
				}
				ctx!!.update()
			},
			drawRect() {
DCloud-yyl's avatar
DCloud-yyl 已提交
266
				let ctx = (this.$refs['draw-odd'] as Element).getDrawableContext()
张磊 已提交
267 268 269
				ctx!!.reset()
				// Create path
				ctx!!.moveTo(30, 90);
张磊 已提交
270 271 272 273 274 275
				ctx!.lineTo(110, 20);
				ctx!.lineTo(240, 130);
				ctx!.lineTo(60, 130);
				ctx!.lineTo(190, 20);
				ctx!.lineTo(270, 90);
				ctx!.closePath();
张磊 已提交
276 277

				// Fill path
张磊 已提交
278 279
				ctx!.fillStyle = "green";
				ctx!.fill("evenodd");
张磊 已提交
280 281 282 283 284 285 286 287 288
				ctx!!.update()

			}
		}
	}
</script>

<style>

张磊 已提交
289
</style>