forked from javierbyte/img2css
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageAsCanvas.js
More file actions
46 lines (35 loc) · 965 Bytes
/
ImageAsCanvas.js
File metadata and controls
46 lines (35 loc) · 965 Bytes
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
import React from 'react'
var ImageAsCanvas = React.createClass({
propTypes: {
base64image: React.PropTypes.string
},
shouldComponentUpdate (nextProps) {
return nextProps.base64image !== this.props.base64image
},
componentDidMount () {
if (this.props.base64image) this._loadImageToCanvas()
},
componentDidUpdate () {
this._loadImageToCanvas()
},
_loadImageToCanvas () {
var img = new window.Image()
img.onload = () => {
var canvas = React.findDOMNode(this.refs.canvas)
canvas.width = img.width
canvas.height = img.height
var ctx = canvas.getContext('2d')
ctx.drawImage(img, 0, 0)
console.warn(ctx.getImageData(0, 0, ctx.canvas.width, ctx.canvas.height))
// do some manipulations...
// canvas.toDataURL('image/png')
}
img.src = this.props.base64image
},
render () {
return (
<canvas ref='canvas' />
)
}
})
module.exports = ImageAsCanvas