|
|
@@ -1,19 +1,18 @@
|
|
|
import Taro from '@tarojs/taro';
|
|
|
-import { Canvas, View } from '@tarojs/components';
|
|
|
+import { Canvas, View, Image } from '@tarojs/components';
|
|
|
import { useEffect, useRef, useState } from 'react';
|
|
|
import React from 'react';
|
|
|
+import './Dial.scss'
|
|
|
import { useSelector } from 'react-redux';
|
|
|
|
|
|
const Component = (props) => {
|
|
|
const [scenario] = useState(useSelector((state: any) => state.scenario))
|
|
|
const [currentContext, setCurrentContext] = useState(null)
|
|
|
- const canvasWidth: number = 200;
|
|
|
- const canvasHeight: number = 200;
|
|
|
- const circleRadius: number = 100;
|
|
|
- const ringWidth: number = 30;
|
|
|
- const dotRadius: number = 10;
|
|
|
+ const canvasWidth: number = 334;
|
|
|
+ const canvasHeight: number = 334;
|
|
|
const dpr = Taro.getSystemInfoSync().pixelRatio; // 获取设备的像素比
|
|
|
-
|
|
|
+ const canvasRef = useRef<any>(null);
|
|
|
+ const watchLineWidth = 300
|
|
|
const canvasId = 'canvasDialId';
|
|
|
|
|
|
var canStartDrag = false;
|
|
|
@@ -38,7 +37,7 @@ const Component = (props) => {
|
|
|
var strEnd = current.end;
|
|
|
|
|
|
var needDrawFastRing = false;
|
|
|
- if (scenario.name == 'FAST_SLEEP' && scenario.step == 'sleep'){
|
|
|
+ if (scenario.name == 'FAST_SLEEP' && scenario.step == 'sleep') {
|
|
|
needDrawFastRing = true;
|
|
|
}
|
|
|
|
|
|
@@ -51,6 +50,7 @@ const Component = (props) => {
|
|
|
const _canvas = res[0].node;
|
|
|
_canvas.width = res[0].width * dpr;
|
|
|
_canvas.height = res[0].height * dpr;
|
|
|
+ global.canvas = _canvas
|
|
|
const ctx = _canvas.getContext('2d');
|
|
|
setCurrentContext(ctx)
|
|
|
|
|
|
@@ -65,34 +65,63 @@ const Component = (props) => {
|
|
|
const drawCanvas = (ctx: any) => {
|
|
|
|
|
|
|
|
|
- // 绘制圆环
|
|
|
+
|
|
|
const centerX = canvasWidth / 2;
|
|
|
const centerY = canvasHeight / 2;
|
|
|
- const radius = Math.min(centerX, centerY) - 10;
|
|
|
+ const radius = 140;//Math.min(centerX, centerY) - 38;
|
|
|
|
|
|
ctx.clearRect(0, 0, canvasWidth, canvasHeight);
|
|
|
|
|
|
- if(needDrawFastRing){
|
|
|
+ //画断食环
|
|
|
+ if (needDrawFastRing) {
|
|
|
ctx.beginPath();
|
|
|
- ctx.arc(centerX, centerY, radius+8, global.fast_start_angle, global.fast_end_angle);
|
|
|
- ctx.strokeStyle = 'rgba(170, 255, 0, 0.3)';
|
|
|
- ctx.lineWidth = 2;
|
|
|
- ctx.stroke();
|
|
|
+ ctx.arc(centerX, centerY, radius + 16 + 8, global.fast_start_angle, global.fast_end_angle);
|
|
|
+ ctx.strokeStyle = 'rgba(170, 255, 0, 0.3)';
|
|
|
+ ctx.lineWidth = 2;
|
|
|
+ ctx.stroke();
|
|
|
}
|
|
|
|
|
|
+ // 绘制圆环
|
|
|
ctx.beginPath();
|
|
|
ctx.arc(centerX, centerY, radius, 0, 2 * Math.PI);
|
|
|
ctx.strokeStyle = "rgba(0,0,0,0.5)";
|
|
|
- ctx.lineWidth = 10;
|
|
|
+ ctx.lineWidth = 38;
|
|
|
ctx.stroke();
|
|
|
|
|
|
+ //绘制起终点圆弧
|
|
|
ctx.beginPath();
|
|
|
ctx.arc(centerX, centerY, radius, startAngle, endAngle);
|
|
|
- ctx.strokeStyle = canRingDrag ? "red" : "rgba(255,0,0,0.5)";
|
|
|
- ctx.lineWidth = 10;
|
|
|
+ ctx.strokeStyle = canRingDrag ? "#3e3e3e" : "#1c1c1c";
|
|
|
+ ctx.lineWidth = 38;
|
|
|
+ ctx.lineCap = "round";
|
|
|
ctx.stroke();
|
|
|
|
|
|
- if (scenario.step == 'fast'){
|
|
|
+
|
|
|
+ //画刻度线
|
|
|
+ ctx.save()
|
|
|
+ ctx.beginPath()
|
|
|
+ ctx.moveTo(centerX, centerY)
|
|
|
+
|
|
|
+ ctx.arc(
|
|
|
+ centerX, centerY, watchLineWidth / 2.0, startAngle, endAngle
|
|
|
+ )
|
|
|
+
|
|
|
+ ctx.clip()
|
|
|
+ if (global.canvas_watch_line) {
|
|
|
+ ctx.drawImage(global.canvas_watch_line, centerX - watchLineWidth / 2.0, centerY - watchLineWidth / 2.0, watchLineWidth, watchLineWidth)
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ let tempImage = global.canvas.createImage()
|
|
|
+ tempImage.src = require('@/assets/images/watch_line.png')//'./watch_line.png'//'..//assets/images/watch_line.png'
|
|
|
+ tempImage.onload = () => {
|
|
|
+ global.canvas_watch_line = tempImage
|
|
|
+ drawCanvas(ctx)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ ctx.restore()
|
|
|
+ ctx.closePath()
|
|
|
+
|
|
|
+ if (scenario.step == 'fast') {
|
|
|
global.fast_start_angle = startAngle;
|
|
|
global.fast_end_angle = endAngle;
|
|
|
}
|
|
|
@@ -100,19 +129,55 @@ const Component = (props) => {
|
|
|
const pointX = centerX + Math.cos(startAngle) * radius;
|
|
|
const pointY = centerY + Math.sin(startAngle) * radius;
|
|
|
|
|
|
+ //绘制起点圆点
|
|
|
ctx.beginPath();
|
|
|
- ctx.arc(pointX, pointY, 5, 0, 2 * Math.PI);
|
|
|
- ctx.fillStyle = '#00ff00';
|
|
|
+ ctx.arc(pointX, pointY, 17, 0, 2 * Math.PI);
|
|
|
+ ctx.fillStyle = (canRingDrag || canStartDrag) ? "#3e3e3e" : "#1c1c1c";
|
|
|
ctx.fill();
|
|
|
|
|
|
- // 绘制蓝色终点圆点
|
|
|
+ //绘制起点图标
|
|
|
+ // ctx.save()
|
|
|
+ // ctx.beginPath()
|
|
|
+ // ctx.translate(pointX, pointY)
|
|
|
+ // ctx.rotate(startAngle)
|
|
|
+ // ctx.translate(-pointX, -pointY)
|
|
|
+
|
|
|
+ if (global.canvas_start_icon) {
|
|
|
+ ctx.drawImage(global.canvas_start_icon, pointX - 20, pointY - 20, 40, 40)
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ let tempImage = global.canvas.createImage()
|
|
|
+ tempImage.src = require('@/assets/images/wakeup-white.png')//'./watch_line.png'//'..//assets/images/watch_line.png'
|
|
|
+ tempImage.onload = () => {
|
|
|
+ global.canvas_start_icon = tempImage
|
|
|
+ drawCanvas(ctx)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // ctx.restore()
|
|
|
+ // ctx.closePath()
|
|
|
+
|
|
|
+ // 绘制终点圆点
|
|
|
const pointX2 = centerX + Math.cos(endAngle) * radius;
|
|
|
const pointY2 = centerY + Math.sin(endAngle) * radius;
|
|
|
|
|
|
ctx.beginPath();
|
|
|
- ctx.arc(pointX2, pointY2, 5, 0, 2 * Math.PI);
|
|
|
- ctx.fillStyle = 'blue';
|
|
|
+ ctx.arc(pointX2, pointY2, 17, 0, 2 * Math.PI);
|
|
|
+ ctx.fillStyle = (canRingDrag || canEndDrag) ? "#3e3e3e" : "#1c1c1c";
|
|
|
ctx.fill();
|
|
|
+
|
|
|
+ //绘制终点图标
|
|
|
+ if (global.canvas_end_icon) {
|
|
|
+ ctx.drawImage(global.canvas_end_icon, pointX2 - 20, pointY2 - 20, 40, 40)
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ let tempImage = global.canvas.createImage()
|
|
|
+ tempImage.src = require('@/assets/images/watch_end_white.png')//'./watch_line.png'//'..//assets/images/watch_line.png'
|
|
|
+ tempImage.onload = () => {
|
|
|
+ global.canvas_end_icon = tempImage
|
|
|
+ drawCanvas(ctx)
|
|
|
+ }
|
|
|
+ }
|
|
|
};
|
|
|
|
|
|
|
|
|
@@ -301,7 +366,8 @@ const Component = (props) => {
|
|
|
const pointX = centerX + Math.cos(startAngle) * radius;
|
|
|
const pointY = centerY + Math.sin(startAngle) * radius;
|
|
|
const distance = Math.sqrt(Math.pow(canvasX - pointX, 2) + Math.pow(canvasY - pointY, 2));
|
|
|
- if (distance <= 12) {
|
|
|
+ console.log('distance' + distance)
|
|
|
+ if (distance <= 40) {
|
|
|
canStartDrag = true;
|
|
|
} else {
|
|
|
canStartDrag = false;
|
|
|
@@ -310,11 +376,21 @@ const Component = (props) => {
|
|
|
const pointX2 = centerX + Math.cos(endAngle) * radius;
|
|
|
const pointY2 = centerY + Math.sin(endAngle) * radius;
|
|
|
const distance2 = Math.sqrt(Math.pow(canvasX - pointX2, 2) + Math.pow(canvasY - pointY2, 2));
|
|
|
- if (distance2 <= 12) {
|
|
|
+ console.log('distance2' + distance2)
|
|
|
+ if (distance2 <= 40) {
|
|
|
canEndDrag = true;
|
|
|
} else {
|
|
|
canEndDrag = false;
|
|
|
}
|
|
|
+
|
|
|
+ if (canStartDrag && canEndDrag) {
|
|
|
+ if (distance < distance2) {
|
|
|
+ canEndDrag = false
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ canStartDrag = false
|
|
|
+ }
|
|
|
+ }
|
|
|
lastDuration = durationAngle(endAngle, startAngle);
|
|
|
|
|
|
if (canStartDrag || canEndDrag) {
|
|
|
@@ -326,7 +402,7 @@ const Component = (props) => {
|
|
|
const distance3 = twoPointDistance({ x: canvasX, y: canvasY }, { x: centerX, y: centerY });
|
|
|
let angle = Math.atan2(canvasY - canvasWidth / 2.0, canvasX - canvasWidth / 2.0)
|
|
|
|
|
|
- if (distance3 > 70 && distance3 < 105 && isCenterAngle(startAngle, angle, endAngle)) {
|
|
|
+ if (distance3 > 120 && distance3 < 190 && isCenterAngle(startAngle, angle, endAngle)) {
|
|
|
canRingDrag = true;
|
|
|
}
|
|
|
lastAngle = angle;
|
|
|
@@ -369,13 +445,17 @@ const Component = (props) => {
|
|
|
handleTouchMove(e)
|
|
|
}
|
|
|
|
|
|
- return <View style={{ width: 200, height: 200, }} ref={props.ref}>
|
|
|
+ return <View style={{ width: 334, height: 334, borderRadius: 167, backgroundColor: '#000', position: 'relative' }} ref={props.ref}>
|
|
|
<Canvas canvasId={canvasId} id={canvasId} className={canvasId} type="2d"
|
|
|
- style={{ width: 200, height: 200, zIndex: 0 }}
|
|
|
+ style={{ width: 334, height: 334, zIndex: 0 }}
|
|
|
onTouchMove={handleTouchMove}
|
|
|
onTouchEnd={handleTouchEnd}
|
|
|
onTouchStart={handleClick}
|
|
|
+ ref={canvasRef}
|
|
|
/>
|
|
|
+ <View className='inner'>
|
|
|
+ <Image src={require('@/assets/images/watch-inner.png')} style={{ width: 226, height: 226 }} />
|
|
|
+ </View>
|
|
|
</View>
|
|
|
}
|
|
|
|