การวาดรูปภาพด้วย KonvaJS

เพื่อที่จะสร้างรูปภาพด้วย KonvaJS นั้น  เราจะใช้ Konva.Image()ครับ. สำหรับรายละเอียดวิธีการใช้แอตทริบิวต์และเมธอดสามารถดูได้จากที่นี่ครับ API Document : http://konvajs.github.io/docs/shapes/Image.html


ตัวอย่าง แทคที่ใช้
ตัวอย่าง : วาดรูปภาพ
      
      var stage = new Konva.Stage({
        container: 'container',
        width: 578,
        height: 200
      });
      var layer = new Konva.Layer();

      var imageObj = new Image();
      imageObj.onload = function() {
        var nop = new Konva.Image({
          x: 140,                        //กำหนดจุดวาดตามแนวแกน x
          y: stage.getHeight() / 2 - 59, //กำหนดจุดวาดตามแนวแกน y
          image: imageObj,               // อ็อปเจ็คของรูป
          width: 106,                    // ความกว้างของรูป
          height: 158                    // ความสูงของรูป
        });

        var filteredNop = new Konva.Image({
          x: 320,                        //กำหนดจุดวาดตามแนวแกน x
          y: stage.getHeight() / 2 - 59, //กำหนดจุดวาดตามแนวแกน y
          image: imageObj,               // อ็อปเจ็คของรูป
          width: 106,                    // ความกว้างของรูป
          height: 158                    // ความสูงของรูป
        });

        // เพิ่มรูปเข้าไปใน layer
        layer.add(nop);
        layer.add(filteredNop);

        // เพิ่ม layer เข้าไปใน stage
        stage.add(layer);

        // ทำ filter ให้เป็นสีเทาให้กับรูปที่สอง
        filteredNop.applyFilter(Konva.Filters.Grayscale, null, function() {
          layer.draw();
        });
      };
      // กำหนดที่อยู่ของรูป     
      imageObj.src = '//lh6.googleusercontent.com/-iap-aKTMRFE/UgnMNYslekI/AAAAAAAAFBg/PryfPMgFWh4/w310-h544-no/photo.jpg';

ผลลัพธ์ที่ได้คือ

About Nop

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment

0 comments:

Post a Comment