Skip to content

Commit

Permalink
fix IE/Edge SVG animation bug (chanind#165)
Browse files Browse the repository at this point in the history
  • Loading branch information
chanind authored Mar 5, 2020
1 parent be6bb60 commit 2f136f9
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 6 deletions.
2 changes: 1 addition & 1 deletion demo/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ function updateCharacter() {
writer = HanziWriter.create('target', character, {
width: 400,
height: 400,
renderer: 'canvas',
radicalColor: '#166E16',
onCorrectStroke: printStrokePoints,
onMistake: printStrokePoints,
showCharacter: false,
});
isCharVisible = true;
isOutlineVisible = true;
Expand Down
14 changes: 10 additions & 4 deletions src/renderers/svg/CharacterRenderer.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const { isMsBrowser } = require('../../utils');
const StrokeRenderer = require('./StrokeRenderer');


Expand All @@ -18,10 +19,15 @@ CharacterRenderer.prototype.render = function(props) {
if (props === this._oldProps) return;
if (props.opacity !== this._oldProps.opacity) {
this._group.style.opacity = props.opacity;
if (props.opacity === 0) {
this._group.style.display = 'none';
} else if (this._oldProps.opacity === 0) {
this._group.style.display = 'initial';
// MS browsers seem to have a bug where if SVG is set to display:none, it sometimes breaks.
// More info: https://github.com/chanind/hanzi-writer/issues/164
// this is just a perf improvement, so disable for MS browsers
if (!isMsBrowser) {
if (props.opacity === 0) {
this._group.style.display = 'none';
} else if (this._oldProps.opacity === 0) {
this._group.style.removeProperty('display');
}
}
}
const colorsChanged = (
Expand Down
2 changes: 1 addition & 1 deletion src/renderers/svg/__tests__/CharacterRenderer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ describe('CharacterRenderer', () => {
charRenderer.render(props2);

expect(subCanvas.style.opacity).toBe('0.9');
expect(subCanvas.style.display).toBe('initial');
expect(subCanvas.style.display).toBe('');
});

});
4 changes: 4 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ const objRepeat = (item, times) => {
return obj;
};

const ua = (global.navigator && global.navigator.userAgent) || '';
const isMsBrowser = ua.indexOf('MSIE ') > 0 || ua.indexOf('Trident/') > 0 || ua.indexOf('Edge/') > 0;

module.exports = {
_assign,
arrLast,
Expand All @@ -137,4 +140,5 @@ module.exports = {
requestAnimationFrame,
timeout,
trim,
isMsBrowser,
};

0 comments on commit 2f136f9

Please sign in to comment.