Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Supporting SVG renderer changes for subpixel bounds #127

Closed
wants to merge 6 commits into from

Conversation

adroitwhiz
Copy link
Contributor

Resolves

Half of resolving scratchfoundation/scratch-render#427

Proposed Changes

  • Remove the old, unused _draw and fromString methods
  • Make measurements public and add adjustedRotationCenter, adjustedSize, and adjustedMeasurements
  • Remove loadSVG, make createSVGImage public, and add setSubpixelViewbox
  • Remove viewOffset

Reason for Changes

These changes implement the SVG side of the changes described in scratchfoundation/scratch-render#550. They allow for the bounding box and rotation center of a loaded SVG to be adjusted so that it can be rendered at an integer size and integer coordinates.

The supporting API changes give scratch-render more control over what happens when, and were done as necessary to make things function in scratch-render. These more tightly couple scratch-render and this repo, but I'm not sure how else to implement this stuff--any suggestions on reducing coupling are greatly appreciated.

Test Coverage

Still needs some discussion

To do

  • Remove adjustedSize and just grab width/height from adjustedMeasurements
  • See if any other places in the codebase use SvgRenderer.size or if the renderer can grab that from measurements

@adroitwhiz
Copy link
Contributor Author

This contains potentially breaking changes-- revisit scratchfoundation/scratch-vm#1650 or check that the API surface used by other repos is unaffected

Copy link
Contributor

@fsih fsih left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: we reviewed up to 4a41b78


const svgData = "<svg>...</svg>";
const scale = 1;
const quirksMode = false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should try to document what this means

@@ -58,8 +58,9 @@
}

function renderSVGString(str) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't pass in str

// Reset the canvas transform after drawing.
this._context.setTransform(1, 0, 0, 1, 0, 0);
// Set the CSS style of the canvas to the actual measurements.
this._canvas.style.width = bbox.width;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: this was added in scratchfoundation/scratch-render@b35f684
and we don't know why, and it doesn't seem relevant anymore

*/
this.adjustedSize = null;

this.adjustedMeasurements = {xOffset: 0, yOffset: 0, width: 0, height: 0};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Document

* Null when an SVG is not loaded.
* @type {?Array<number>}
*/
this.adjustedRotationCenter = null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's think about how we can move the very scratch-render-api specific functions back into Render to make sure that other dependencies aren't accidentally affected


const measurements = this.measurements;

// Compensate for quirks-mode SVG viewbox offset.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove reference to "quirks-mode" here, since that's sprite2 specific

/**
* Set the loaded SVG's viewBox and this renderer's viewOffset for proper subpixel positioning.
* @param {Array<number>} rotationCenter The rotation center of the Skin this SVG is used for.
* @param {number} minScale The minimum scale at which this SVG should be rendered with proper subpixel positioning.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds like this is specifically the minimum mipmap scale

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, the scale needs to be 1/2^x form


this.adjustedSize = adjustedSize;

this.adjustedMeasurements.xOffset = ((1 - centerFrac[0]) / minScale);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add commenting

// and with integer dimensions at power-of-two sizes down to minScale.
this._svgTag.setAttribute('viewBox',
`${
measurements.x - ((1 - centerFrac[0]) / minScale)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change to use adjustedMeasurements

@adroitwhiz
Copy link
Contributor Author

This logic can now all be moved back into scratch-render

@adroitwhiz adroitwhiz closed this Mar 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants