summaryrefslogtreecommitdiff
path: root/client/assets/javascripts/mx/primitives/mx.cutout.js
blob: 9d9043facd522c0678b05e5e4ca68042ae337d1a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
MX.Cutout = MX.Object3D.extend({
  init: function (ops) {
  
  	this.type = "Cutout"

    var layer = this
    layer.width = 0
    layer.height = 0

    if (ops.src) this.loadTexture(ops)

		if (ops.texture) {
		}
		else if (ops.classname) {
			layer.el.classList.add(ops.classname)
		}
		else {
		}
		layer.el.style.backgroundRepeat = 'no-repeat'

    this.dirty = true
    this.updateChildren = true
    this.update()
  },

  loadTexture: function(ops){
    var layer = this
    var image = new Image()
    var pattern = ops.pattern
    var texture = ops.texture
    
    image.onload = function(){
			var canvas = document.createElement("canvas")
			var ctx = canvas.getContext('2d')

			layer.width = canvas.width = image.naturalWidth
			layer.height = canvas.height = image.naturalHeight

			ctx.drawImage(image, 0, 0, canvas.width, canvas.height)
			ctx.globalCompositeOperation = "source-in"
			
			if (texture) {
				ctx.fillStyle = ctx.createPattern(texture, 'repeat')
				ctx.fillRect(0, 0, canvas.width, canvas.height)
			}
			if (pattern) {
				ctx.fillStyle = ctx.createPattern(pattern, 'repeat')
				ctx.fillRect(0, 0, canvas.width, canvas.height)
			}

			layer.scale = ops.scale || 1
			layer.x = ops.x || 0
			layer.y = (ops.y || 0) + layer.height/2 + 1
			layer.z = ops.z || 0
			layer.rotationX = ops.rotationX || 0
			layer.rotationY = ops.rotationY || 0
			layer.el.appendChild(canvas)

			layer.el.classList.add('image')
			ops.className && layer.el.classList.add(ops.className)
			layer.dirty = true
			layer.update()
    }
    image.src = ops.src;
  }
})