qt qml 刮刮卡效果

用canvas+mouseArea实现的刮刮卡效果。

表层是一层色彩,用手指划开,可看到下面的文字
Lisence: MIT, 请保留本文档说明
Author: surfsky.cnblogs.com 2015-02

【先看效果】

 

【下载】

http://download.csdn.net/detail/surfsky/8445011

【核心代码】

 1     Canvas {
 2         id: canvas
 3         anchors.fill: parent
 4 
 5         //
 6         property bool isFirstPaint : true;
 7         property point lastPt;
 8 
 9         //
10         onPaint: {
11             var ctx = getContext('2d');
12             if (isFirstPaint){
13                 // 首次绘制刮刮层图案
14                 ctx.fillStyle = root.maskColor;
15                 ctx.fillRect(0, 0, width, height);
16                 isFirstPaint = false;
17             }
18             else{
19                 // 去除鼠标位置的图案
20                 clearRound(ctx, lastPt, 20);
21                 lastPt = Qt.point(area.mouseX, area.mouseY);
22             }
23         }
24 
25         // 清除圆形区域
26         function clearRound(ctx, p, r)
27         {
28             ctx.save();
29             ctx.globalCompositeOperation = 'destination-out';
30             ctx.beginPath();
31             ctx.arc(p.x, p.y, r, 0, 2*Math.PI, false);
32             ctx.fill();
33             ctx.restore();
34         }
35 
36         // 记录下最后的鼠标点,并请求canvas重绘
37         MouseArea {
38             id: area
39             anchors.fill: parent
40             onPressed: {canvas.lastPt = Qt.point(mouseX, mouseY);}
41             onPositionChanged: {canvas.requestPaint();}
42         }
43     }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值