Comments (28)
I also have this issue.
Anyone have any idea?
Thanks in advance.
from react-native-canvas.
The following declaration of ImageData works:
import {webviewConstructor, webviewProperties} from './webview-binders';
@webviewProperties({
data: null,
height: 100,
width: 100
})
@webviewConstructor('ImageData')
export default class ImageData {
constructor(width, height, canvas) {
this.canvas = canvas;
if (this.onConstruction) {
this.onConstruction(width, height);
}
}
postMessage = message => {
if (this.canvas)
return this.canvas.postMessage(message);
};
addMessageListener = listener => {
if (this.canvas)
return this.canvas.addMessageListener(listener);
};
}
But i need ImageData with pixels data inside to be able use it like this:
context.getImageData(0, 0, 100, 100)
.then((imageData) => {
const data = imageData.data;
const length = Object.keys(data).length;
for(let i = 0; i < length; i += 4) {
data[i] += 100;
data[i + 1] += 100;
data[i + 2] += 100;
}
const tmp = new ImageData(data, 100, 100, canvas); // Or Uint8ClampedArray.from(data) here
context.putImageData(tmp, 0, 0);
});
I tried to modify ImageData declaration to the following:
constructor(array, width, height, canvas) {
...
if (this.onConstruction) {
this.onConstruction(array, width, height);
}
...
}
But this does not work. I receive error
Do you have any idea why this might happens? Thanks
from react-native-canvas.
Ok, I'll look at this as soon as I have the time.
from react-native-canvas.
For anyone interested and waiting for this, I can't make time to prioritize this PR, please solve this issue on your own or contact @iddan for guidance.
from react-native-canvas.
If anyone wants to help checkout #49
from react-native-canvas.
You get a plain working object from getImageData. putImageData expects and object of type imageData. For that you need to invoke it with the ref of the result object from getImageData
from react-native-canvas.
Same here!
And I got this error.
Anyone else?
Error: The operation is insecure
from react-native-canvas.
Currently the ImageData interface is not implemented thus no, you can't
from react-native-canvas.
Hi iddan, thanks for a great lib!
Im confused about this since I see you using this in your examples.
Cant get const image = new CanvasImage(this.reds.myCanvas) to work
from react-native-canvas.
ImageData and Image are different interfaces
from react-native-canvas.
Hi, yeah I noticed that some days after my comment.
from react-native-canvas.
A PR will be welcome (basically implement the interface and connect it to the relevant components). Unfortunately I would not have the time to do it any time soon.
from react-native-canvas.
Hi, been mega busy lately and I don't know when things are gonna clear up, still since this is a vital dependency in my application Iยดm gonna try to make time. Any example you can point me to to ease the process? looked at your source code and couldn't say I found an obvious way of implementing this.
from react-native-canvas.
In https://github.com/iddan/react-native-canvas/blob/master/src/Image.js:
@webviewProperties({crossOrigin: undefined, height: undefined, src: undefined, width: undefined})
@webviewEvents(['load', 'error'])
@webviewConstructor('Image')
Those are used to declare shared properties of the Image interface at the RN side and the WebView side.
constructor(canvas, width, height) {
if (!(canvas instanceof Canvas)) {
throw new Error('Image must be initialized with a Canvas instance');
}
this.canvas = canvas;
if (this.onConstruction) {
this.onConstruction();
}
// ...
}
postMessage = message => {
return this.canvas.postMessage(message);
};
addMessageListener = listener => {
return this.canvas.addMessageListener(listener);
};
These make sure the constructed Image can send messages and receive messages from the canvas
In https://github.com/iddan/react-native-canvas/blob/master/src/webview.js:
const constructors = {
Image,
is for declaring Image is constructible
from react-native-canvas.
Hi @iddan thanks for the explanation! looked at it yesterday and got confused cause const data = await ctx.getImageData(0,0,100,100) seems to be working. Made a small canvas with a purple pixel and the data returned matches the rgba values of the pixel correctly.
So are some of the imageData methods implemented and some not? Didn't work with putImageData - tried to use it like: this.refs.otherCanvas.getContext('2d').putImageData(data)
from react-native-canvas.
Hello.
Thank you for this great package. Could you please point me to some spots how to implement putImageData() functionality? What i'm currently doing is:
I'm trying to extend Canvas with a function that will call putImageData() for context like this:
(0, _webviewBinders.webviewMethods)(['toDataURL', 'autoScale', 'putImageData'])
and this
{
key: 'putImageData',
value: function putImageData() {
var context = this.element.getContext('2d');
context.putImageData.apply(context, arguments));
}
}
If i add trace log to handleMessage function of Canvas - i receive the following trace:
Could you please point me what i'm doing wrong. Need this functionality asap. Thanks
from react-native-canvas.
Hey there. As I mentioned before the first thing you need to do is to implement an ImageData constructor: (look for the @constructor
decorator) and declare it in webview.js constructors. That way when shared between the webview and the RN runtime they will be aware for this object and it's state. Then as you done you'd have to add putImageData to webviewMethods.
To test your code you need to execute npm run build-to-examples
in the root directory and then to try your code in the examples RN app.
from react-native-canvas.
Thank you for your quick reply. I'm trying to implement this approach.
I'm trying to add ImageData class as you wrote. I added the following class:
import {webviewConstructor} from './webview-binders';
@webviewConstructor('ImageData')
export default class ImageData {
constructor(canvas, ...args) {
this.canvas = canvas;
if (this.onConstruction) {
this.onConstruction(...args);
}
}
postMessage = message => {
return this.canvas.postMessage(message);
};
addMessageListener = listener => {
return this.canvas.addMessageListener(listener);
};
}
I added it to constructors in webview.js as you said:
const constructors = {
Image,
Path2D,
ImageData,
};
I build src to dest and copy dest folder to examples/node_modules/react-native-canvas folder.
Then i try to create instance of this class like this: const tmp = new ImageData(canvas);
But i receive the following error:
Could you point me what i'm doing wrong and what i missed? Thanks
from react-native-canvas.
Everything you written you've done correctly
from react-native-canvas.
It seems the problem is in this lines:
if (this.onConstruction) {
this.onConstruction(...args);
}
I receive the following error if they exist:
If i comment out these lines - error is gone. But if i try to use ImageData now as argument for putImageData function like this:
const tmp = new ImageData(canvas);
context.putImageData(tmp, 0, 0)
I receive the following error:
Do you have any idea what might be wrong?
from react-native-canvas.
Very simple: ImageData requires arguments https://developer.mozilla.org/en-US/docs/Web/API/ImageData/ImageData
Try to provide (array, width, height) or (width, height)
from react-native-canvas.
Can you PR this so I can try debugging it?
from react-native-canvas.
Sorry, it seems i don't have permissions to push new branch, so i can't create PR. I sent you patch file to email address from your profile on github
from react-native-canvas.
HI @iddan, I need getImageData
interface but version 0.2.0
is not yet published to npm. When do you plan to release it?
from react-native-canvas.
Hey @bbhopesh You can already with the latest version
from react-native-canvas.
Can I? I am using version 0.1.37
I am using getImageData
to get RGBA values of every pixel. Look the code snippet below. Object returned by getImageData doesn't have data
, width
or height
properties which are standard in browser's canvas.
import Canvas, {Image as CanvasImage} from 'react-native-canvas'
class MyComponent extends React.Component {
constructor(props) {
super(props);
}
handleCanvas = (canvas) => {
const ctx = canvas.getContext('2d');
ctx.fillStyle = 'purple';
ctx.fillRect(0, 0, 100, 100);
// Object printed by following line doesn't have any 'data', 'width' or 'height' field.
console.log(ctx.getImageData(0, 0, 100, 100))
}
render() {
return (<View style={styles.sectionContainer}>
<Canvas ref={this.handleCanvas}/>
</View>);
}
}
const App: () => React$Node = () => {
return (
<>
<MyComponent></MyComponent>
</>);
}
const styles = StyleSheet.create({
sectionContainer: {
marginTop: 32,
paddingHorizontal: 24,
}
});
export default App;
from react-native-canvas.
Never mind, I followed the example app and realized getImageData
returns a Promise.
from react-native-canvas.
Hi @iddan ,
Can you please help me fix the issue that I am facing right now, correct me if I am wrong. Would save my lot of time and effort.
I am doing this on my mobile and it perfectly draws image on canvas with same width and height.
current width: 384 and height: 377
But when I am getting imageData from getImageData, it only gives me partial image data for quarter width and height.
It is working fine but not for the full image, my image is having effects partially.
Can you please suggest what I am doing wrong or if it is a bug itself?
This is my code snippet below:
` image.addEventListener('load', async (image) => {
context.drawImage(image.target, 0, 0, canvas.width, canvas.height);
const imageData = await context.getImageData(0, 0, canvas.width, canvas.height);
const data = imageData.data;
console.log(imageData.width)
const dataArr = Object.values(data);
const dataSize = dataArr.length;
for (let idx = 0; idx < dataSize; idx += 4) {
var avg = (dataArr[idx] + dataArr[idx + 1] + dataArr[idx + 2]) / 3;
dataArr[idx] = avg; // red
dataArr[idx + 1] = avg; // green
dataArr[idx + 2] = avg; // blue
}
const tmp = new ImageData(canvas, dataArr, 384, 377);
context.putImageData(tmp, 0, 0);
});`
Thanks in advance!
from react-native-canvas.
Related Issues (20)
- why Why did I use getImageData() for more than 1 minute? HOT 2
- Canvas remain black on windows
- How do you get fillRect to occupy full dimensions? HOT 4
- ImageData/getImageData/putImageData strange behaviour HOT 2
- error HOT 2
- New architecture support for fabric HOT 1
- require cycle node_modules\react-native-canvas HOT 1
- canvas.loadFromJSON missing in react-native-canvas
- Error: Unable to resolve module ./CanvasRenderingContext2D HOT 1
- Not able to add fontFamily with font
- Application gets hang after 10 mins due to drawing on canvas continuously
- Can't draw image greater than 2048x2048 HOT 1
- Virtual/In-Memory Canvas HOT 1
- Can't draw Image on release HOT 1
- Doesn't work at all with Expo HOT 5
- Does not work with Expo 50 HOT 1
- `clearRect()` crashes in Expo 49 HOT 1
- does not work in react-native-windows
- How to draw local image on canvas using react-native-canvas HOT 1
- Setting width/height of canvas doesn't work HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-native-canvas.