uniapp-x·绘制图形线条

uniapp-x·view标签 绘制画线条

<view class="content" id="canvas" 
 @touchstart="changeStart" 
 @touchmove="changeMove"
 @touchend="changeEnd"></view>
     
let listTouche = ref<any>([]);
    const changeStart = (res : any) => {
        const touch = res.changedTouches[0];
        // CTX.reset();//重置
        CTX.beginPath();
        CTX.moveTo(touch.screenX, touch.screenY+ scrollTop);

        // 设置虚线样式
        CTX.setLineDash([5, 3]); // 5px实线,3px空白
        CTX.lineWidth = 2; // 线宽
        CTX.strokeStyle = "black"; // 线条颜色
        
        listTouche.value[listTouche.value.length - 1].push({ x: touch.clientX, y: touch.clientY });
    };

    const changeMove = (res : any) => {
        const touch = res.changedTouches[0];
        CTX.lineTo(touch.screenX, touch.screenY+ scrollTop);
        CTX.stroke(); // 实时绘制
        CTX.update(); // 实时更新
        listTouche.value[listTouche.value.length - 1].push({ x: touch.clientX, y: touch.clientY });
    };

    const changeEnd = () => {
        listTouche.value.push([]);
    };
 
 let scrollTop = 0; //使用标签<scroll-view @scroll="changeScroll" 则必须使用scroll辅助坐标
    const changeScroll = (res : any) => {
        scrollTop = res.detail.scrollTop - 50;
    }
分享你的喜爱
linwute
linwute

我要像梦一样自由,像大地一样宽容;
在艰辛放逐的路上,点亮生命的光芒;
我要像梦一样自由,像天空一样坚强;
在曲折蜿蜒的路上,体验生命的意义;

留下评论

您的邮箱地址不会被公开。 必填项已用 * 标注