Skip to content

Commit f66a485

Browse files
committed
export racing bar video
1 parent a55523a commit f66a485

File tree

2 files changed

+39
-23
lines changed

2 files changed

+39
-23
lines changed

src/pages/ContentScripts/features/repo-activity-racing-bar/RacingBar.tsx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ import { avatarColorStore } from './AvatarColorStore';
33

44
import React, { useEffect, useState, useRef } from 'react';
55
import * as echarts from 'echarts';
6-
import type { EChartsOption, EChartsType, BarSeriesOption } from 'echarts';
76
import { Spin } from 'antd';
8-
import { mediaRecorder, recordingStarted, setRecordingStarted } from './view';
7+
import type { BarSeriesOption, EChartsOption, EChartsType } from 'echarts';
8+
import { stopRecording } from './view';
9+
910
interface RacingBarProps {
1011
repoName: string;
1112
data: RepoActivityDetails;
@@ -152,12 +153,11 @@ const play = (instance: EChartsType, data: RepoActivityDetails) => {
152153
}
153154
if (i == months.length - 1) {
154155
// Stop the media recorder after the animation finishes
155-
instance.on('finished', function () {
156-
if (recordingStarted) {
157-
setRecordingStarted(false);
158-
mediaRecorder.stop();
159-
}
160-
});
156+
// instance.on('finished', function () {
157+
stopRecording();
158+
const rec = document.getElementById('rec');
159+
if(rec!=null) rec.innerText = 'record'
160+
// });
161161
}
162162
};
163163

src/pages/ContentScripts/features/repo-activity-racing-bar/view.tsx

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,22 @@ interface Props {
1313
currentRepo: string;
1414
repoActivityDetails: RepoActivityDetails;
1515
}
16-
export let recordingStarted = false;
17-
export function setRecordingStarted(status: boolean) {
18-
recordingStarted = status;
19-
}
16+
17+
let recordingStarted = false;
2018
export let mediaRecorder: MediaRecorder;
19+
export function stopRecording() {
20+
console.log('before stop media ' + recordingStarted);
21+
if (recordingStarted) {
22+
recordingStarted = false;
23+
console.log('stop media');
24+
mediaRecorder.stop();
25+
console.log('------- ed -------');
26+
const rec = document.getElementById('rec');
27+
// @ts-ignore
28+
rec.innerText = 'record';
29+
30+
}
31+
}
2132
const View = ({ currentRepo, repoActivityDetails }: Props): JSX.Element => {
2233
const [options, setOptions] = useState<HypercrxOptions>(defaults);
2334
const [replay, setReplay] = useState(0);
@@ -32,30 +43,34 @@ const View = ({ currentRepo, repoActivityDetails }: Props): JSX.Element => {
3243
setReplay(replay + 1);
3344
};
3445

35-
recordingStarted = false;
36-
46+
3747
// Event listener for the start button
3848
const exportToVideo = () => {
3949
// Start the recording only if it has not already started
4050
if (!recordingStarted) {
4151
recordingStarted = true;
4252
// Capture the canvas element
43-
44-
const canvas = document.querySelector(
45-
'#hypercrx-repo-activity-racing-bar > div > div > div.d-flex.flex-wrap.flex-items-center > div.col-12.col-md-8 > div > div > div > div > canvas'
46-
) as HTMLCanvasElement;
53+
console.log('start media ' + recordingStarted);
54+
const canvas = document.querySelector('#hypercrx-repo-activity-racing-bar > div > div > div.d-flex.flex-wrap.flex-items-center > div.col-12.col-md-8 > div > div > div > div > div > div > canvas') as HTMLCanvasElement;
55+
const rec = document.getElementById('rec');
56+
57+
if (!canvas || !rec) {
58+
return;
59+
}
60+
61+
rec.innerText = '录制中,点击停止录制并下载';
4762
const stream = canvas.captureStream();
48-
63+
4964
// Start the media recorder
5065
const chunks: Blob[] = [];
5166
mediaRecorder = new MediaRecorder(stream, {
5267
mimeType: 'video/webm; codecs=vp9',
5368
});
54-
69+
5570
mediaRecorder.ondataavailable = function (event) {
5671
chunks.push(event.data);
5772
};
58-
73+
handleReplayClick();
5974
// Start recording
6075
mediaRecorder.start();
6176

@@ -67,7 +82,6 @@ const View = ({ currentRepo, repoActivityDetails }: Props): JSX.Element => {
6782
// Create a video element and set the source to the recorded video
6883
const video = document.createElement('video');
6984
video.src = url;
70-
7185
// Download the video
7286
const a = document.createElement('a');
7387
a.download = 'chart_animation.webm';
@@ -77,6 +91,8 @@ const View = ({ currentRepo, repoActivityDetails }: Props): JSX.Element => {
7791
// Clean up
7892
URL.revokeObjectURL(url);
7993
};
94+
95+
rec.onclick = stopRecording;
8096
}
8197
};
8298

@@ -98,7 +114,7 @@ const View = ({ currentRepo, repoActivityDetails }: Props): JSX.Element => {
98114
)}
99115
</button>
100116

101-
<button className="replay-button" onClick={exportToVideo}>
117+
<button id="rec" className="replay-button" onClick={exportToVideo}>
102118
Record
103119
</button>
104120
</div>

0 commit comments

Comments
 (0)