当我旋转一个正方形时,我想从旋转点计算 4 个偏移量。
旋转轴最初位于正方形的左上角。当我执行旋转时,我想知道形状将在所有 4 个方向(minX、minY、maxX、maxy)上传播多远。
我目前有一般数学:
const rotation = .35 // radians = 20 degrees
const size = 50 // size of original square
const o1 = Math.round(size * Math.sin(rotation))
const o2 = Math.round(size * Math.cos(rotation))
Run Code Online (Sandbox Code Playgroud)
使用这些数字,我看到了如何使用它们来创建偏移量数组
const offsets = [o1, 0, o2, o1 + o2]
Run Code Online (Sandbox Code Playgroud)
当我将正方形从 20、110、200 和 290 度旋转时,它将围绕图像上黑点标记的轴旋转。
对于 4 次旋转中的每一次,我都有 offests 数组以及我想要的实际数字。正如您所看到的,这些数字在那里,但是......我最初认为我只需要一个数组移位,但它不止于此。
// 20 degrees
console.log(offsets) // [17, 0, 47, 64]
// The dimensions I actually need
// minX: -17,
// minY: 0
// maxX: …Run Code Online (Sandbox Code Playgroud) 在尝试在事件内进行异步调用时遇到了挑战。
这是来自Nodemailer的代码-我在需要进行异步调用的行中添加了这一行:
let transporter = nodemailer.createTransport({
SES: new aws.SES({
apiVersion: '2010-12-01'
}),
sendingRate: 1 // max 1 messages/second
});
// Push next messages to Nodemailer
transporter.on('idle', () => {
while (transporter.isIdle()) {
// I need to make an async db call to get the next email in queue
const mail = await getNextFromQueue()
transporter.sendMail(mail);
}
});
Run Code Online (Sandbox Code Playgroud)
我发现了这篇文章建议进行一些有意义的切换,但是我无法正确地将其应用于此。
更新 -答案是使用Sinon模拟sendMail。
好吧我放弃...
设为selectorSettings时,如何使唯一出现?selectorStrategytournament
selectorStrategy: joi.string().valid(['tournament', 'roulette']).default('tournament'),
selectorSettings: joi.any().when('selectorStrategy', {
is: 'tournament',
then: joi.object().keys({
tournamentSize: joi.number().integer().default(2),
baseWeight: joi.number().integer().default(1)
})
})
Run Code Online (Sandbox Code Playgroud)
我已经stripUnknown: true设定了选项。我的期望是,如果我通过:
selectorStrategy: 'roulette',
selectorSettings: { tournamentSize: 3 }
Run Code Online (Sandbox Code Playgroud)
我会得到:
selectorStrategy: 'roulette'
Run Code Online (Sandbox Code Playgroud)
如果我这样做:
selectorStrategy: 'tournament'
Run Code Online (Sandbox Code Playgroud)
我会得到:
selectorStrategy: 'tournament',
selectorSettings: { tournamentSize: 2, baseWeight: 1 }
Run Code Online (Sandbox Code Playgroud) 我正在尝试在现有的reudx API中集成redux-simple-auth。我的旧实现使用本机,fetch并添加了标头等。此新模块提供了访存替换功能,但它返回了redux操作,并且在弄清楚如何进行设置方面有些费劲。
我的商店:
function configureStore (initialState) {
const middlewares = [
authMiddleware, // This is from rediux-simple-auth
apiMiddleware, // My own middleware
thunk
]
const store = createStore(rootReducer, initialState, compose(
applyMiddleware(...middlewares), getInitialAuthState({ storage }))
)
}
Run Code Online (Sandbox Code Playgroud)
我的中间件简化了:
import { fetch } from 'redux-simple-auth'
export default store => next => action => {
...
next(my start action...)
return store.dispatch(fetch(API_ROOT + endpoint, { method }))
.then(response => {
next(my success/fail action...)
})
}
Run Code Online (Sandbox Code Playgroud)
当我运行此命令时,我可以在redux检查器中看到我的启动和失败操作,但不能看到获取操作(它确实触发了FETCH操作)
如果我打电话next而不是打电话,store.dispatch …
奇怪的行为 - 我试图在回调中打开一个新窗口 - 使用Angular但可能是一般的JS问题.
如果我这样做:
$window.open('http://google.com', '_blank');
Run Code Online (Sandbox Code Playgroud)
它工作正常.然而,这不是因为它被我的浏览器阻止 - 我正在使用Safari 7并检查了"阻止弹出窗口":
Items.list(function(items) {
$window.open('http://google.com', '_blank')
});
Run Code Online (Sandbox Code Playgroud)
为什么浏览器会阻止而不阻止另一个,我该如何规避?我玩setTimeout以及一些SO帖子建议在调用异步之前将$ window.open分配给变量但在这里不起作用.
javascript ×2
node.js ×2
amazon-mws ×1
angularjs ×1
asynchronous ×1
eventemitter ×1
joi ×1
reactjs ×1
redux ×1
rotation ×1
trigonometry ×1