Skip to content

Commit

Permalink
Canvas should be more reliable in 2024
Browse files Browse the repository at this point in the history
And RIP Opera.
  • Loading branch information
nanaya committed Dec 8, 2024
1 parent fcfad29 commit ff8220b
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 18 deletions.
17 changes: 4 additions & 13 deletions app/javascript/src/classes/browser_view.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,9 @@ export default class BrowserView
@last_preload_request_active = false
@img_box = @container.down('.image-box')

# In Opera 10.63, the img.complete property is not reset to false after changing the
# src property. Blits from images to the canvas silently fail, with nothing being
# blitted and no exception raised. This causes blank canvases to be displayed, because
# we have no way of telling whether the image is blittable or if the blit succeeded.
if !Prototype.Browser.Opera
@canvas = create_canvas_2d()
if @canvas
@canvas.hide()
@img_box.appendChild @canvas
@canvas = document.createElement('canvas')
@canvas.hide()
@img_box.appendChild @canvas
@zoom_level = 0

# True if the post UI is visible.
Expand Down Expand Up @@ -493,8 +487,7 @@ export default class BrowserView
return
@img = document.createElement('img')
@img.className = 'main-image'
if @canvas
@canvas.hide()
@canvas.hide()
@img.show()

###
Expand Down Expand Up @@ -1092,8 +1085,6 @@ export default class BrowserView
if !@img.fully_loaded
console.debug "image incomplete; can't render to canvas"
return false
if !@canvas
return

# If the contents havn't changed, skip the blit. This happens frequently when resizing
# the window when not fitting the image to the screen.
Expand Down
2 changes: 1 addition & 1 deletion app/javascript/src/classes/thumbnail_user_image.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ $ = jQuery
export default class ThumbnailUserImage
constructor: (file, onComplete) ->
@file = file
@canvas = create_canvas_2d()
@canvas = document.createElement('canvas')
@image = document.createElement('img')
@onComplete = onComplete
@url = URL.createObjectURL(@file)
Expand Down
4 changes: 0 additions & 4 deletions app/javascript/src/legacy/common.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,6 @@ window.InitAdvancedEditing = ->
window.distance_squared = (x1, y1, x2, y2) ->
(x1 - x2) ** 2 + (y1 - y2) ** 2

### If 2d canvases are supported, return one. Otherwise, return null. ###
window.create_canvas_2d = ->
document.createElement('canvas')

Prototype.Browser.AndroidWebKit = navigator.userAgent.indexOf('Android') != -1 and navigator.userAgent.indexOf('WebKit') != -1

### Some UI simply doesn't make sense on a touchscreen, and may need to be disabled or changed.
Expand Down

0 comments on commit ff8220b

Please sign in to comment.