Canvas Konva框架庫

  • 第三方的canvas庫

  • Konva 是一個 基于 Canvas 開發(fā)的 2d js 框架庫, 它可以輕松的實現(xiàn)桌面應用和移動應用中的圖形交互交互效果.

konva概念

  • 舞臺 stage
  • 層 layer
  • 組 group
  • 圖形

基本操作

  • 創(chuàng)建舞臺
var stage = new Konva.Stage({
            container:"box",
            width:1000,
            height:600
        });
  • 創(chuàng)建層
var layer = new Konva.Layer();
        stage.add(layer);
...........
layer.draw();  //繪制層

基本圖形

  • Konva.Rect() 矩形
var rect = new Konva.Rect({
            x:100,
            y:100,
            width:200,
            height:100,
            fill:"red",
            stroke:"green",
            strokeWidth:10,
            cornerRadius:5,
            //rotation:45
            draggable:true
        })
        layer.add(rect);
  • Konva.Circle() 圓形
var circle = new Konva.Circle({
            x:300,
            y:300,
            radius:100,
            stroke:"orange",
            strokeWidth:4,
            fill:"green",
            draggable:true,
            dash:[10,5,20,5]
        })
        layer.add(circle);
  • Konva.Wedge() 扇形
var wedge = new Konva.Wedge({
            x:500,
            y:500,
            radius:100,
            fill:"yellow",
            stroke:"green",
            angle:60,
            rotation:90
        })
        layer.add(wedge);
  • Konva.Ellipse() 橢圓
  • Konva.Text() 文字
var text = new Konva.Text({
            x:700,
            y:30,
            text:"小萍萍\n您好",
            fontSize:60,
            fontFamily:"MicrosoftYaHei",
            fill:"green",
            stroke:"red"
        })
        layer.add(text);
  • Konva.Image() 圖片
  • Konva.Line() 劃線
var line = new Konva.Line({
            points:[10,10,500,10,500,200],
            stroke:"green",
            strokeWidth:3,
            dash:[10,5],
            closed:true
        });

        layer.add(line);
  • Konva.star() 星星
  var star = new Konva.Star({
            x:600,
            y:400,
            innerRadius:50,
            outerRadius:100,
            numPoints:12,
            fill:"red"
        });
        layer.add(star);
  • 繪制曲線
        var line2 = new Konva.Line({
            points:[20,200,500,200,400,400],
            stroke:"blue",
            strokeWidth:5,
            tension:-2

        })
        layer.add(line2);

事件

類似于jquery

var rect = new Konva.Rect({
            x:100,
            y:100,
            width:200,
            height:200,
            fill:"red",
            stroke:"green",
            strokeWidth:5,
            cornerRadius:5,     
        })
        layer.add(rect);

        rect.on("mouseenter", function(){
            this.rotation(45);
            layer.draw();
        }).on("mouseleave", function(){
            this.rotation(0);
            layer.draw();
        })
        layer.draw();

動畫

  • tween對象 指定動畫持續(xù)時間 指定變化的樣式 tween.play()
//持續(xù)轉動 動畫
        var star = new Konva.Star({
            numPoints:10,
            x:500,
            y:200,
            innerRadius:50,
            outerRadius:100,
            fill:"red"
        })
        layer.add(star);
        var tween1 = new Konva.Tween({
            node:star,
            duration:1,
            rotation:360,
            onFinish:function(){
                tween1.reset();
                tween1.play();
            }
        });
        tween1.play();
  • 簡寫 圖形.to()
    var rect2 = new Konva.Rect({
    x:400,
    y:400,
    width:100,
    height:50,
    stroke:"green",
    fill:"red"
    })
    layer.add(rect2);
    rect2.to({
    duration:1,
    width:200,
    height:100
    })
  • Animation對象
//持續(xù)轉動 動畫
        var star = new Konva.Star({
            numPoints:5,
            x:500,
            y:200,
            innerRadius:50,
            outerRadius:100,
            fill:"red"
        })
        layer.add(star);        
        //Animation對象
        var animate = new Konva.Animation(function(frame){
            //console.log(frame);
            star.rotate(10);
        }, layer);
        animate.start();

//進度條案例

    var stage=new Konva.Stage({
            container:"box",
            width:window.innerWidth,
            height:window.innerHeight
        })
        var layer=new Konva.Layer()
        stage.add(layer)
        var width=800;
        var progressValue=new Konva.Rect({
            x:200,
            y:200,
            width:0,
            height:50,
            fill:"#abcdef"
        })
        layer.add(progressValue)
        var progressLine=new Konva.Rect({
            x:200,
            y:200,
            width:width,
            height:50,
            stroke:"#369",
            strokeWidth:4,
            cornerRadius:5
        })
        layer.add(progressLine)
        layer.draw()
        var animation=new Konva.Animation(function(){
            if(progressValue.width()<width){
                progressValue.width(progressValue.width()+10)
            }else{
                animation.stop()
            }
        },layer)
        animation.start()
最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

  • 一:canvas簡介 1.1什么是canvas? ①:canvas是HTML5提供的一種新標簽 ②:HTML5 ...
    GreenHand1閱讀 4,874評論 2 32
  • 【四、Canvas開發(fā)庫封裝】 《4.1封裝常用的繪制函數(shù)》 4.1.1封裝一個矩形 //思考:我們用到的矩形需要...
    夜幕小草閱讀 4,951評論 2 5
  • 一、canvas簡介 1.1 什么是canvas?(了解) 是HTML5提供的一種新標簽 Canvas是一個矩形區(qū)...
    Looog閱讀 4,034評論 3 40
  • drhjrjj
    奇翼果手機樂隊閱讀 210評論 0 0
  • 昨晚是我美學形象管理課程的結業(yè)儀式,盡管只是線上短短的幾個月,但是讓人既激動又不舍,百感交集,五味雜陳。 緣起 很...
    柳桃小姐閱讀 375評論 0 2

友情鏈接更多精彩內(nèi)容