cm-chessboard 5.5.10 → 5.5.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
<h2>Example: RenderVideo extension</h2>
|
|
13
13
|
<p>Works in Chrome (webm) and Safari (mp4).</p>
|
|
14
14
|
<div class="board board-small" id="board"></div>
|
|
15
|
-
<video id="videoOut" controls="controls"
|
|
15
|
+
<video id="videoOut" controls="controls"></video>
|
|
16
16
|
<script type="module">
|
|
17
17
|
import {RenderVideo} from "../../src/cm-chessboard/extensions/render-video/RenderVideo.js"
|
|
18
18
|
import {Chessboard} from "../../src/cm-chessboard/Chessboard.js"
|
|
@@ -28,21 +28,18 @@
|
|
|
28
28
|
props: {}
|
|
29
29
|
}]
|
|
30
30
|
})
|
|
31
|
-
chessboard.
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
}, 400)
|
|
44
|
-
})
|
|
45
|
-
}, 250)
|
|
31
|
+
await chessboard.recorderInit()
|
|
32
|
+
await chessboard.recorderStart()
|
|
33
|
+
await chessboard.recorderPause(500)
|
|
34
|
+
await chessboard.movePiece("e2", "e4", true)
|
|
35
|
+
await chessboard.recorderPause(500)
|
|
36
|
+
await chessboard.movePiece("e7", "e5", true)
|
|
37
|
+
await chessboard.recorderPause(1000)
|
|
38
|
+
chessboard.recorderStop().then((url) => {
|
|
39
|
+
const video = document.getElementById("videoOut")
|
|
40
|
+
video.src = url
|
|
41
|
+
})
|
|
42
|
+
|
|
46
43
|
</script>
|
|
47
44
|
</body>
|
|
48
45
|
</html>
|
package/package.json
CHANGED
|
@@ -26,45 +26,61 @@ export class RenderVideo extends Extension {
|
|
|
26
26
|
}
|
|
27
27
|
}
|
|
28
28
|
console.log("recorder mediaType", this.props.mediaType)
|
|
29
|
-
this.
|
|
29
|
+
this.image = new Image()
|
|
30
|
+
document.body.append(this.image)
|
|
30
31
|
this.makeSpriteInline()
|
|
31
|
-
this.registerExtensionPoint(EXTENSION_POINT.animation, (
|
|
32
|
+
this.registerExtensionPoint(EXTENSION_POINT.animation, () => {
|
|
32
33
|
if (this.recorder && this.recorder.state === "recording") {
|
|
33
34
|
setTimeout(() => {
|
|
34
35
|
this.cloneImageAndRender()
|
|
35
36
|
})
|
|
36
37
|
}
|
|
37
38
|
})
|
|
39
|
+
this.registerMethod("recorderInit", () => {
|
|
40
|
+
return new Promise((resolve) => {
|
|
41
|
+
let all = document.querySelectorAll("svg *")
|
|
42
|
+
for (let i = 0; i < all.length; i++) {
|
|
43
|
+
this.transferComputedStyle(all[i])
|
|
44
|
+
}
|
|
45
|
+
const dimensions = this.chessboard.view.svg.getBBox()
|
|
46
|
+
this.canvas = document.createElement("canvas")
|
|
47
|
+
this.canvas.style.backgroundColor = "#000"
|
|
48
|
+
this.canvas.style.display = "none"
|
|
49
|
+
this.canvas.width = dimensions.width
|
|
50
|
+
this.canvas.height = dimensions.height
|
|
51
|
+
this.context = this.canvas.getContext('2d')
|
|
52
|
+
document.body.append(this.canvas)
|
|
53
|
+
|
|
54
|
+
this.image = new Image()
|
|
55
|
+
this.image.width = this.canvas.width
|
|
56
|
+
this.image.height = this.canvas.height
|
|
57
|
+
this.image.style.position = "absolute"
|
|
58
|
+
this.image.style.visibility = "hidden"
|
|
59
|
+
document.body.append(this.image)
|
|
60
|
+
|
|
61
|
+
this.stream = this.canvas.captureStream()
|
|
62
|
+
this.recorder = new MediaRecorder(this.stream, {mimeType: this.props.mediaType})
|
|
63
|
+
this.recorder.ondataavailable = (event) => {
|
|
64
|
+
if (event.data && event.data.size) {
|
|
65
|
+
this.recordedData.push(event.data)
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
setTimeout(() => {
|
|
69
|
+
resolve()
|
|
70
|
+
}, 100)
|
|
71
|
+
})
|
|
72
|
+
})
|
|
38
73
|
this.registerMethod("recorderStart", () => {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
let all = document.querySelectorAll("svg *")
|
|
44
|
-
for (let i = 0; i < all.length; i++) {
|
|
45
|
-
this.transferComputedStyle(all[i])
|
|
46
|
-
}
|
|
47
|
-
const dimensions = this.chessboard.view.svg.getBBox()
|
|
48
|
-
this.canvas = document.createElement("canvas")
|
|
49
|
-
this.canvas.style.backgroundColor = "#000"
|
|
50
|
-
this.canvas.style.display = "none"
|
|
51
|
-
this.canvas.width = dimensions.width
|
|
52
|
-
this.canvas.height = dimensions.height
|
|
53
|
-
this.contect = this.canvas.getContext('2d')
|
|
54
|
-
document.body.append(this.canvas)
|
|
55
|
-
this.stream = this.canvas.captureStream()
|
|
56
|
-
this.recorder = new MediaRecorder(this.stream, {mimeType: this.props.mediaType})
|
|
57
|
-
this.recordedData = []
|
|
58
|
-
this.recorder.ondataavailable = (event) => {
|
|
59
|
-
if (event.data && event.data.size) {
|
|
60
|
-
this.recordedData.push(event.data)
|
|
74
|
+
return new Promise((resolve) => {
|
|
75
|
+
if (this.recorder && this.recorder.state === "recording") {
|
|
76
|
+
console.error("recorder is running")
|
|
77
|
+
return
|
|
61
78
|
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
}
|
|
67
|
-
console.log("recorder", this.recorder.state)
|
|
79
|
+
this.recordedData = []
|
|
80
|
+
this.recorder.start()
|
|
81
|
+
console.log("recorder", this.recorder.state)
|
|
82
|
+
resolve()
|
|
83
|
+
})
|
|
68
84
|
})
|
|
69
85
|
/**
|
|
70
86
|
* returns the url of the recorded media
|
|
@@ -74,12 +90,25 @@ export class RenderVideo extends Extension {
|
|
|
74
90
|
if (!this.recorder || this.recorder.state !== "recording") {
|
|
75
91
|
reject("recorder is not recording")
|
|
76
92
|
}
|
|
77
|
-
this.
|
|
93
|
+
this.cloneImageAndRender()
|
|
78
94
|
setTimeout(() => {
|
|
79
95
|
this.recorder.stop()
|
|
80
96
|
console.log("recorder", this.recorder.state)
|
|
81
97
|
resolve(URL.createObjectURL(new Blob(this.recordedData, {type: this.props.mediaType})))
|
|
82
|
-
},
|
|
98
|
+
}, 200)
|
|
99
|
+
})
|
|
100
|
+
})
|
|
101
|
+
this.registerMethod("recorderPause", (ms) => {
|
|
102
|
+
return new Promise((resolve) => {
|
|
103
|
+
this.cloneImageAndRender()
|
|
104
|
+
const interval = setInterval(() => {
|
|
105
|
+
this.cloneImageAndRender()
|
|
106
|
+
}, 20)
|
|
107
|
+
setTimeout(() => {
|
|
108
|
+
clearInterval(interval)
|
|
109
|
+
this.cloneImageAndRender()
|
|
110
|
+
resolve()
|
|
111
|
+
}, ms)
|
|
83
112
|
})
|
|
84
113
|
})
|
|
85
114
|
}
|
|
@@ -88,19 +117,11 @@ export class RenderVideo extends Extension {
|
|
|
88
117
|
let serialized = new XMLSerializer().serializeToString(this.chessboard.view.svg)
|
|
89
118
|
const blob = new Blob([serialized], {type: "image/svg+xml"})
|
|
90
119
|
const blobURL = URL.createObjectURL(blob)
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
image.width = this.canvas.width
|
|
95
|
-
image.height = this.canvas.height
|
|
96
|
-
image.style.position = "absolute"
|
|
97
|
-
image.style.visibility = "hidden"
|
|
98
|
-
//
|
|
99
|
-
document.body.append(image)
|
|
100
|
-
image.onload = () => {
|
|
101
|
-
this.contect.drawImage(image, 0, 0, this.canvas.width, this.canvas.height, 0, 0, this.canvas.width, this.canvas.height)
|
|
120
|
+
this.image.onload = () => {
|
|
121
|
+
this.context.drawImage(this.image, 0, 0, this.canvas.width, this.canvas.height, 0, 0, this.canvas.width, this.canvas.height)
|
|
122
|
+
this.recorder.requestData()
|
|
102
123
|
}
|
|
103
|
-
image.src = blobURL
|
|
124
|
+
this.image.src = blobURL
|
|
104
125
|
}
|
|
105
126
|
|
|
106
127
|
transferComputedStyle(element) {
|