qmlcanvas,类Html5 canvas的组件

http://jamiesun.iteye.com/blog/846748


qml的画板组件,实验室产品,好事者可以git clone git://gitorious.org/qt-labs/qmlcanvas.git 

然后qmake && make 

看看效果如何 

这是一个类似html5中的canvas组件。 
qmlcanvas,类Html5 canvas的组件_第1张图片 

上面其实是一个动态画面截取的一张,效果是一个类似星空扩散的动画. 

完全是用javascript+qml写出来的. 

balls.js 

Javascript代码   收藏代码
  1. var tId;  
  2. var cHeight = 0;  
  3. var cWidth = 0;  
  4. var numBalls = 100;  
  5. var balls = new Array();  
  6.   
  7. function Ball(){  
  8.     this.xPos = 0;  
  9.     this.yPos = 0;  
  10.     this.vx = 0;  
  11.     this.vy = 0;  
  12.     this.radius = 5;  
  13.     this.colour;  
  14.     this.wind;  
  15.     this.gravity;  
  16.     this.sizeWobble;  
  17.   
  18.     this.reset = function() {  
  19.         this.xPos = cWidth / 2;  
  20.         this.yPos = cHeight / 2;  
  21.         this.vx = Math.random() * 10 - 5;  
  22.         this.vy = Math.random() * 10 - 5;  
  23.         this.colour = randomColour();  
  24.         this.wind = Math.random() * 2 - 1;  
  25.         this.gravity = Math.random() * 2 - 1;  
  26.         this.radius = Math.random() * 20 + 5;  
  27.         this.sizeWobble = Math.random() * 2 - 1;  
  28.     }  
  29. }  
  30.   
  31. function init(w, h) {  
  32.   
  33.     // Set canvas values  
  34.     cHeight = w  
  35.     cWidth = h;  
  36.   
  37.     // Generate balls  
  38.     for(var i = 0;i < numBalls;i++){  
  39.         balls.push(new Ball());  
  40.         balls[i].reset();  
  41.     }  
  42. }  
  43.   
  44. function drawBalls(){  
  45.     var ctx = getContext();  
  46.     for(var i = 0;i < numBalls;i++){  
  47.         drawCircle(balls[i].xPos, balls[i].yPos, balls[i].radius, balls[i].colour);  
  48.         balls[i].vy += balls[i].gravity;  
  49.         balls[i].vx += balls[i].wind;  
  50.         balls[i].xPos += balls[i].vx;  
  51.         balls[i].yPos += balls[i].vy;  
  52.         if(balls[i].radius > 2){  
  53.             balls[i].radius += balls[i].sizeWobble;  
  54.         }  
  55.   
  56.         if(  
  57.                 balls[i].xPos - balls[i].radius > cWidth||  
  58.                 balls[i].xPos + balls[i].radius < 0||  
  59.                 balls[i].yPos - balls[i].radius > cHeight||  
  60.                 balls[i].yPos + balls[i].radius < 0  
  61.                 ){  
  62.             balls[i].reset();  
  63.         }  
  64.     }  
  65. }  
  66.   
  67. function randomColour(){  
  68.     var red = Math.round(Math.random() * 255);  
  69.     var green = Math.round(Math.random() * 255);  
  70.     var blue = Math.round(Math.random() * 255);  
  71.     return "rgb(" + red + ", " + green + ", " + blue + ")";  
  72. }  
  73.   
  74. function drawCircle(cx, cy, radius, colour){  
  75.     var ctx = getContext();      
  76.     ctx.fillStyle = colour;  
  77.     ctx.strokeStyle = "rgb(60, 80, 50)";  
  78.     ctx.beginPath();  
  79.     ctx.arc(cx,cy,radius,0,Math.PI*2,true); // Outer circle  
  80.     ctx.fill();  
  81.     ctx.stroke();  
  82. }  


balls.qml 

import "../../Canvas" 是导入上层目录中编译好的插件 
import "balls.js" as Balls 直接导入js脚本 

Qml代码   收藏代码
  1. import "../../Canvas"   
  2. import Qt 4.7  
  3.   
  4. import "balls.js" as Balls  
  5.   
  6. Canvas {  
  7.     id: clock  
  8.     width:500  
  9.     height:400  
  10.       
  11.     Timer {  
  12.         interval: 50; running: true; repeat: true  
  13.         onTriggered: draw();  
  14.     }  
  15.   
  16.     function draw() {  
  17.         var ctx = getContext();  
  18.         ctx.clearRect(00, width, height);  
  19.         Balls.drawBalls(ctx);  
  20.     }  
  21.   
  22.     Component.onCompleted: Balls.init(width, height)  
  23. }  


qml的声明式脚本比 javafx,flex好用多了

你可能感兴趣的:(canvas,qt)