reworked image copy to take account image orientation (working around webkit bug)...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2020-11-04 17:36:13 +03:00
parent 409e82cb26
commit 06fa563d04

View File

@ -29,6 +29,90 @@ var widgets = require('features/ui-widgets')
/*********************************************************************/
// helpers...
var img2canvas =
function({url, orientation, flipped}, callback){
var img = new Image
// XXX PATCH...
// XXX <canvas>.drawImage(..) seems to take EXIF into account, ignoring
// the .imageOrientation setting both the canvas and image are in
// DOM and the image needs to be added to dom before .src is set
var PATCHED_ELEMENTS
var PATCH_BUG = function(e){
PATCHED_ELEMENTS =
PATCHED_ELEMENTS
|| document.body.appendChild(
document.createElement('div')
.run(function(){
this.style.position = 'absolute'
this.style.with = '0'
this.style.height = '0'
this.style.top = '200%'
this.style.left = '0'
this.style.opacity = 0
this.style.overflow = 'hidden' }))
PATCHED_ELEMENTS.appendChild(e) }
var CLEANUP_PATCH = function(){
PATCHED_ELEMENTS
.parentElement
.removeChild(PATCHED_ELEMENTS) }
// XXX END PATCH...
img.onload = function(){
var width = this.naturalWidth
var height = this.naturalHeight
var c = document.createElement('canvas')
c.style.imageOrientation = 'none'
// XXX PATCH...
PATCH_BUG(c)
var ctx = c.getContext('2d')
// prepare for rotate...
// 90 / 270
if(orientation == 90 || orientation == 270){
var w = c.width = this.naturalHeight
var h = c.height = this.naturalWidth
// 0 / 180
} else {
var w = c.width = this.naturalWidth
var h = c.height = this.naturalHeight }
// prepare for flip...
var x = flipped && flipped.includes('horizontal') ?
-1
: 1
var y = flipped && flipped.includes('vertical') ?
-1
: 1
ctx.translate(w/2, h/2)
ctx.rotate(orientation * Math.PI/180)
ctx.scale(x, y)
ctx.drawImage(this, -width/2, -height/2)
// XXX PATCH...
CLEANUP_PATCH()
callback.call(this, c) }
// prevent the browser from rotating the image via exif...
img.style.imageOrientation = 'none'
img.crossOrigin = ''
img.src = url
// XXX PATCH...
PATCH_BUG(img)
return img }
/*********************************************************************/ /*********************************************************************/
var Widget = var Widget =
@ -347,12 +431,28 @@ var ElectronHostActions = actions.Actions({
`, `,
function(size){ function(size){
/* XXX this does not rotate the image...
var url = this.images.getBestPreview(this.current, size, true).url var url = this.images.getBestPreview(this.current, size, true).url
electron.clipboard.write({ electron.clipboard.write({
title: this.images.getImageFileName(), title: this.images.getImageFileName(),
text: url, text: url,
image: electron.nativeImage.createFromPath(url), image: electron.nativeImage.createFromPath(url),
}) }], })
//*/
var that = this
var url = this.images.getBestPreview(this.current, size, true).url
img2canvas({
url,
...this.images[this.current]
}, function(c){
electron.clipboard.write({
title: that.images.getImageFileName(),
text: url,
// XXX this seems not to work with images with exif
// orientation -- the ig orientation seems to be
// ignored...
image: electron.nativeImage.createFromDataURL(c.toDataURL('image/png')),
}) }) }],
paste: ['- Image|Edit/Paste image', paste: ['- Image|Edit/Paste image',
function(){ function(){
// XXX // XXX
@ -436,14 +536,10 @@ var BrowserHostActions = actions.Actions({
NOTE: this will not work with file:// paths... NOTE: this will not work with file:// paths...
`, `,
function(size){ function(size){
var img = new Image; img2canvas({
var c = document.createElement("canvas"); url: this.images.getBestPreview(this.current, size, true).url ,
var ctx = c.getContext("2d"); ...this.images[this.current]
}, function(c){
img.onload = function(){
c.width = this.naturalWidth
c.height = this.naturalHeight
ctx.drawImage(this, 0, 0)
c.toBlob(function(blob){ c.toBlob(function(blob){
// copy... // copy...
// XXX would be nice to add a path/title here... // XXX would be nice to add a path/title here...
@ -451,9 +547,7 @@ var BrowserHostActions = actions.Actions({
new ClipboardItem({ new ClipboardItem({
[blob.type]: blob, [blob.type]: blob,
}) ]) }, }) ]) },
"image/png") } "image/png") })
img.crossOrigin = ''
img.src = this.images.getBestPreview(this.current, size, true).url
}], }],
paste: ['- Image|Edit/Paste image', paste: ['- Image|Edit/Paste image',
function(){ function(){