简体   繁体   English

JavaScript的 <canvas> 绘制缩放图像

[英]JavaScript <canvas> draw scaled image

The amazing Illustrator plugin Ai->canvas exports arbitrary complex Illustrator images to Javascript code that draws on <canvas> . 令人惊奇的Illustrator插件Ai-> canvas将任意复杂的Illustrator图像导出为使用<canvas>绘制的Javascript代码。 For example, this is the code for a green square 150x150 例如,这是绿色正方形150x150的代码

function draw(ctx) {

  // layer4/Path
  ctx.save();
  ctx.beginPath();
  ctx.moveTo(150.0, 150.0);
  ctx.lineTo(0.0, 150.0);
  ctx.lineTo(0.0, 0.0);
  ctx.lineTo(150.0, 0.0);
  ctx.lineTo(150.0, 150.0);
  ctx.closePath();
  ctx.fillStyle = "rgb(0, 255, 0)";
  ctx.fill();
  ctx.restore();
}

This is a stupid example because drawing a square is better done with a rectangle, but anyway, for complex images, this is invaluable. 这是一个愚蠢的例子,因为绘制正方形最好用矩形来完成,但无论如何,对于复杂的图像,这是非常宝贵的。 The problem is scaling . 问题在于扩展

How can I transform a generic draw function output by Ai->canvas in order to accept a scale factor? 如何接受Ai-> canvas转换的通用绘图函数以接受比例因子? This square example is pretty straight forward, but I'd like something more generic to be applied to a Javascript function drawing on canvas (with arcs, circles, bezier curves...). 这个方形示例非常简单,但是我想要一些更通用的东西应用于画布上的Javascript函数绘图(带有弧形,圆形,贝塞尔曲线......)。 Thanks 谢谢

One option is to use the scale function of the canvas, eg: 一种选择是使用画布的scale功能,例如:

ctx.scale(xFactor, yFactor);

More here 更多这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM