Skip to content

Commit 8c4ebdc

Browse files
committed
Emit NativeImage objects in paint event
1 parent 3be68ba commit 8c4ebdc

File tree

3 files changed

+24
-42
lines changed

3 files changed

+24
-42
lines changed

atom/browser/api/atom_api_web_contents.cc

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1337,15 +1337,10 @@ bool WebContents::IsOffScreen() const {
13371337
return type_ == OFF_SCREEN;
13381338
}
13391339

1340-
void WebContents::OnPaint(const gfx::Rect& dirty_rect,
1341-
const SkBitmap& bitmap) {
1342-
v8::MaybeLocal<v8::Object> buffer = node::Buffer::Copy(
1343-
isolate(),
1344-
reinterpret_cast<char*>(bitmap.getPixels()), bitmap.getSize());
1345-
if (!buffer.IsEmpty()) {
1346-
Emit("paint", dirty_rect, buffer.ToLocalChecked(),
1347-
gfx::Size(bitmap.width(), bitmap.height()));
1348-
}
1340+
void WebContents::OnPaint(const gfx::Rect& dirty_rect, const SkBitmap& bitmap) {
1341+
mate::Handle<NativeImage> image =
1342+
NativeImage::Create(isolate(), gfx::Image::CreateFrom1xBitmap(bitmap));
1343+
Emit("paint", dirty_rect, image);
13491344
}
13501345

13511346
void WebContents::StartPainting() {

docs/api/web-contents.md

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -463,33 +463,21 @@ Returns:
463463

464464
* `event` Event
465465
* `dirtyRect` Object
466-
* `x` Number - the x coordinate on the bitmap
467-
* `y` Number - the y coordinate on the bitmap
468-
* `width` Number - the width of the dirty area
469-
* `height` Number - the height of the dirty area
470-
* `data` Buffer - the bitmap data of the dirty rect
471-
* `bitmapSize` Object
472-
* `width` Number - the width of the whole bitmap
473-
* `height` Number - the height of the whole bitmap
466+
* `x` Integer - The x coordinate on the image.
467+
* `y` Integer - The y coordinate on the image.
468+
* `width` Integer - The width of the dirty area.
469+
* `height` Integer - The height of the dirty area.
470+
* `image` [NativeImage](native-image.md) - The image data of the dirty rect
474471

475472
Emitted when a new frame is generated. Only the dirty area is passed in the
476473
buffer.
477474

478475
```javascript
479-
const {BrowserWindow} = require('electron')
480-
481-
let win = new BrowserWindow({
482-
width: 800,
483-
height: 1500,
484-
webPreferences: {
485-
offscreen: true
486-
}
476+
let win = new BrowserWindow({webPreferences: {offscreen: true}})
477+
win.webContents.on('paint', (event, dirty, image) => {
478+
fs.writeSync('frame.png', image.toPNG())
487479
})
488480
win.loadURL('http://github.com')
489-
490-
win.webContents.on('paint', (event, dirty, data) => {
491-
// updateBitmap(dirty, data)
492-
})
493481
```
494482

495483
### Instance Methods

docs/tutorial/offscreen-rendering.md

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,18 @@ const {app, BrowserWindow} = require('electron')
3737

3838
app.disableHardwareAcceleration()
3939

40-
let win = new BrowserWindow({
41-
width: 800,
42-
height: 1500,
43-
webPreferences: {
44-
offscreen: true
45-
}
46-
})
47-
win.loadURL('http://github.com')
48-
49-
win.webContents.setFrameRate(30)
50-
51-
win.webContents.on('paint', (event, dirty, data) => {
52-
// updateBitmap(dirty, data)
40+
let win
41+
app.once('ready', () => {
42+
win = new BrowserWindow({
43+
webPreferences: {
44+
offscreen: true
45+
}
46+
})
47+
win.loadURL('http://github.com')
48+
win.webContents.on('paint', (event, dirty, image) => {
49+
fs.writeSync('frame.png', image.toPNG())
50+
})
51+
win.webContents.setFrameRate(30)
5352
})
5453
```
5554

0 commit comments

Comments
 (0)