Skip to content

Instantly share code, notes, and snippets.

@Bobsans
Created April 1, 2018 14:02
Show Gist options
  • Select an option

  • Save Bobsans/c7c09ad1b6105168d474940d3ce98a4f to your computer and use it in GitHub Desktop.

Select an option

Save Bobsans/c7c09ad1b6105168d474940d3ce98a4f to your computer and use it in GitHub Desktop.
Translate svg path
def translate(path, mx, my):
out = []
parts = path.split(' ')
for part in parts:
if ',' in part:
nums = part.split(',')
prex = nums[0][0] if nums[0][0] in ('M', 'C', 'L') else ''
numx = float(nums[0][1:]) if nums[0][0] in ('M', 'C', 'L') else float(nums[0])
prey = nums[1][0] if nums[1][0] in ('M', 'C', 'L') else ''
numy = float(nums[1][1:]) if nums[1][0] in ('M', 'C', 'L') else float(nums[1])
out.append(f'{prex}{numx - mx:.5f},{numy - my:.5f}')
else:
out.append(part)
return ' '.join(out)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment