forked from mbk-lab/rui_orig
Updated Image for wasm
This commit is contained in:
parent
c0f60e7bdd
commit
8943be8e91
|
@ -1156,6 +1156,37 @@ function loadImage(url) {
|
|||
img.src = url;
|
||||
}
|
||||
|
||||
function loadInlineImage(url, content) {
|
||||
var img = images.get(url);
|
||||
if (img != undefined) {
|
||||
return
|
||||
}
|
||||
|
||||
img = new Image();
|
||||
img.addEventListener("load", function() {
|
||||
images.set(url, img)
|
||||
var message = "imageLoaded{session=" + sessionID + ",url=\"" + url + "\"";
|
||||
if (img.naturalWidth) {
|
||||
message += ",width=" + img.naturalWidth
|
||||
}
|
||||
if (img.naturalHeight) {
|
||||
message += ",height=" + img.naturalHeight
|
||||
}
|
||||
sendMessage(message + "}")
|
||||
}, false);
|
||||
|
||||
img.addEventListener("error", function(event) {
|
||||
var message = "imageError{session=" + sessionID + ",url=\"" + url + "\"";
|
||||
if (event && event.message) {
|
||||
var text = event.message.replace(new RegExp("\"", 'g'), "\\\"")
|
||||
message += ",message=\"" + text + "\"";
|
||||
}
|
||||
sendMessage(message + "}")
|
||||
}, false);
|
||||
|
||||
img.src = content;
|
||||
}
|
||||
|
||||
function clickOutsidePopup(e) {
|
||||
sendMessage("clickOutsidePopup{session=" + sessionID + "}")
|
||||
e.stopPropagation();
|
||||
|
|
29
image.go
29
image.go
|
@ -1,6 +1,12 @@
|
|||
package rui
|
||||
|
||||
import "strconv"
|
||||
import (
|
||||
"encoding/base64"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
// ImageLoading is the image loading status: in the process of loading
|
||||
|
@ -76,6 +82,27 @@ func (manager *imageManager) loadImage(url string, onLoaded func(Image), session
|
|||
image.listener = onLoaded
|
||||
image.loadingStatus = ImageLoading
|
||||
manager.images[url] = image
|
||||
|
||||
if runtime.GOOS == "js" {
|
||||
if file, ok := resources.images[url]; ok && file.fs != nil {
|
||||
dataType := map[string]string{
|
||||
".svg": "data:image/svg+xml",
|
||||
".png": "data:image/png",
|
||||
".jpg": "data:image/jpg",
|
||||
".jpeg": "data:image/jpg",
|
||||
".gif": "data:image/gif",
|
||||
}
|
||||
ext := strings.ToLower(filepath.Ext(url))
|
||||
if prefix, ok := dataType[ext]; ok {
|
||||
if data, err := file.fs.ReadFile(file.path); err == nil {
|
||||
session.callFunc("loadInlineImage", url, prefix+";base64,"+base64.StdEncoding.EncodeToString(data))
|
||||
return image
|
||||
} else {
|
||||
DebugLog(err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
session.callFunc("loadImage", url)
|
||||
return image
|
||||
}
|
||||
|
|
|
@ -119,8 +119,9 @@ func (bridge *wasmBridge) callCanvasVarFunc(v any, funcName string, args ...any)
|
|||
|
||||
func (bridge *wasmBridge) callCanvasImageFunc(url string, property string, funcName string, args ...any) {
|
||||
image := js.Global().Get("images").Call("get", url)
|
||||
if !image.IsUndefined() && !image.IsNull() {
|
||||
result := image.Call(funcName, args...)
|
||||
if !image.IsUndefined() && !image.IsNull() && !bridge.canvas.IsNull() {
|
||||
|
||||
result := bridge.canvas.Call(funcName, append([]any{image}, args...)...)
|
||||
if property != "" {
|
||||
bridge.canvas.Set(property, result)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue