• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
Kevin Cheng's Yard
电脑是我的老婆,编程是我的灵魂,代码是我的语言,按键是我在歌唱。
https://github.com/surfsky/
博客园    首页    新随笔    联系   管理    订阅  订阅

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     }

 

转载请注明出处:http://surfsky.cnblogs.com 

posted @ 2015-02-13 17:31  surfsky  阅读(1425)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3